473,320 Members | 1,920 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Embedding, "import site", PYTHONHOME, and an old, old issue

Well, I've found about a hundred thousand web pages where people have
had the same problem I have but nary a page with a solution that works
for me.

I want to do a simple embed, so I've followed the example in the
Extending and Embedding documentation:

In the .c file,

#include <Python.h>

int routine() {
Py_Initialize();
PyRun_SimpleString("from time import time,ctime\n"
"print 'Today is',ctime(time())\n");
Py_Finalize();
return 0;
}

The code compiles just fine, but when I execute it the call to
Py_Initialize() comes back with:

'import site' failed; use -v for traceback
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named time

I found a lot of websites that say to set PYTHONHOME to the the path to
the directory where site.py lives. I did that but I get the same error.

Here are a few bits o' additional information:

'python -v' tells me it was built with gcc 3.4.4 (and has no trouble at
all finding site.py whether PYTHONHOME is defined or not). The
following code snippet:
>>import distutils.sysconfig
distutils.sysconfig.get_config_var('LINKFORSHARE D')
comes back with '-Xlinker -export-dynamic'.

My own code needs to use Portland Group's pgi. I did some googling for
various permutations of nouns from the preceding few paragraphs and
found Pythonic mention of using "-Wl,-export-dynamic" as a flag for the
PG linker. OK, try that, builds fine, same error.

I cannot recompile Python on this machine and I don't really understand
exactly what is happening with the Py_* function calls in the C snippet
above, or whether I can get more detailed traceback info. This is the
first time I've tried embedding and it's rather obvious that I've run
into a problem that everyone but Messrs. van Rossum and Lundh has hit.
Somebody, somewhere must have an honest-to-glub solution. If you are
that somebody, please let me know what to do because I'm about to throw
in the towel and embed That Other Language.

Oh, one more thing: if I launch python from the shell and type in the
strings from the C snippet it works fine.

Thanks,
Jim
--

It's not "pretexting", it's "lying."
Feb 10 '07 #1
3 6824
Jim Hill (that'd be me) wrote:

I forgot one more key thing: the compiled code is being run via mpirun
(LAM/MPI). Might that have something to do with my pain and heartache?
Jim

(original post reproduced below in shocking breach of etiquette on the
off chance someone's interested in this post and didn't bother reading
the first.)

>Well, I've found about a hundred thousand web pages where people have
had the same problem I have but nary a page with a solution that works
for me.

I want to do a simple embed, so I've followed the example in the
Extending and Embedding documentation:

In the .c file,

#include <Python.h>

int routine() {
Py_Initialize();
PyRun_SimpleString("from time import time,ctime\n"
"print 'Today is',ctime(time())\n");
Py_Finalize();
return 0;
}

The code compiles just fine, but when I execute it the call to
Py_Initialize() comes back with:

'import site' failed; use -v for traceback
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named time

I found a lot of websites that say to set PYTHONHOME to the the path to
the directory where site.py lives. I did that but I get the same error.

Here are a few bits o' additional information:

'python -v' tells me it was built with gcc 3.4.4 (and has no trouble at
all finding site.py whether PYTHONHOME is defined or not). The
following code snippet:
>>import distutils.sysconfig
>>distutils.sysconfig.get_config_var('LINKFORSHARE D')

comes back with '-Xlinker -export-dynamic'.

My own code needs to use Portland Group's pgi. I did some googling for
various permutations of nouns from the preceding few paragraphs and
found Pythonic mention of using "-Wl,-export-dynamic" as a flag for the
PG linker. OK, try that, builds fine, same error.

I cannot recompile Python on this machine and I don't really understand
exactly what is happening with the Py_* function calls in the C snippet
above, or whether I can get more detailed traceback info. This is the
first time I've tried embedding and it's rather obvious that I've run
into a problem that everyone but Messrs. van Rossum and Lundh has hit.
Somebody, somewhere must have an honest-to-glub solution. If you are
that somebody, please let me know what to do because I'm about to throw
in the towel and embed That Other Language.

Oh, one more thing: if I launch python from the shell and type in the
strings from the C snippet it works fine.
--

It's not "pretexting", it's "lying."
Feb 10 '07 #2
En Sat, 10 Feb 2007 03:57:05 -0300, Jim Hill <ji*****@swcp.comescribió:
I want to do a simple embed, so I've followed the example in the
Extending and Embedding documentation:

In the .c file,

#include <Python.h>

int routine() {
Py_Initialize();
PyRun_SimpleString("from time import time,ctime\n"
"print 'Today is',ctime(time())\n");
Py_Finalize();
return 0;
}
(Why routine() and not main()? Unfortunately you can't repeteadly
initialize/finalize the interpreter, you must do that only once.)
The code compiles just fine, but when I execute it the call to
Py_Initialize() comes back with:

'import site' failed; use -v for traceback
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: No module named time
Try this:
PyRun_SimpleString("import sys; print sys.path");
to see where Python expects to find its library (or call the Py_GetPath
function).

You may need to call Py_SetProgramName (before Py_Initialize) so it can
find where the standard library resides.
At least for testing purposes, you can copy your executable into the same
directory where Python is installed.

--
Gabriel Genellina

Feb 10 '07 #3
Gabriel Genellina wrote:
>En Sat, 10 Feb 2007 03:57:05 -0300, Jim Hill <ji*****@swcp.comescribió:
>int routine() {
Py_Initialize();
...
}

(Why routine() and not main()? Unfortunately you can't repeteadly
initialize/finalize the interpreter, you must do that only once.)
This is a small routine tucked off to the side of a fairly large
mostly-FORTRAN-with-some-C program. I need to parse a slash-delimited
input file from a different program and fill up some arrays with the
results. Rather than wrestle with FORTRAN's wretched file I/O I thought
I'd do it this way.
>Try this:
PyRun_SimpleString("import sys; print sys.path");
to see where Python expects to find its library (or call the Py_GetPath
function).
It returned a list of paths nearly identical to what the interactive
interpreter does -- it's on a different machine and too long to retype
here -- the interactive sys.path has an empty string as item 0, while
the embedded sys.path returns the interactive[1:n].
>You may need to call Py_SetProgramName (before Py_Initialize) so it can
find where the standard library resides.
Didn't do anything, alas.
>At least for testing purposes, you can copy your executable into the same
directory where Python is installed.
No can do -- it's not my machine and I don't have appropriate
privileges. Thanks for trying to help me out but I'm on a crash
deadline and it looks like I'll be doing some C parsing. Blech.
Jim
--

It's not "pretexting", it's "lying."
Feb 12 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Vio | last post by:
Hi, I've been trying to embed (statically) wxPy alongside an embedded py interpreter on a linux/gtk box. At one point, for some reason misc.o linking reported "multiple definitions of...
3
by: gerald.maher | last post by:
Hi, I am trying to excuate the follwong code: I get the following error: Here is my Lib path:
0
by: Bill Davy | last post by:
I am working with MSVC6 on Windows XP. I have created an MSVC project called SHIP I have a file SHIP.i with "%module SHIP" as the first line (file is below). I run SHIP.i through SWIG 1.3.24...
2
by: VMI | last post by:
In Access, when a user's going to import a fixed-width format ascii file, a window in the "Import Text Wizard" lets the user "mark" where in a string one field will begin and end (with the vertical...
2
by: Word Painter | last post by:
this may be more of an "html" issue, but I'll wing it. i've got a multi-language site, where the home-page of each language group features a link to a popup window that offers background info on...
0
by: John Pye | last post by:
Hi all, I'm doing some convoluted stuff with running a python script from inside a shared library that's running inside a Tcl/Tk interpreter. It's all been going surprisingly well, up until...
6
by: bhochstetler | last post by:
I am on a hp 11.11 machine doing a 64 bit python 2.5 build. When I get my python executable created and run it, I get the error: "import site failed" OverflowError: signed integer is greater...
1
by: Eric Hanchrow | last post by:
(This is with Python 2.5.2, on Ubuntu Hardy, if it matters.) This seems so basic that I'm surprised that I didn't find anything about it in the FAQ. (Yes, I am fairly new to Python.) Here are...
4
YarrOfDoom
by: YarrOfDoom | last post by:
I just installed wxPython on my computer (Windows "fails-a-lot" Vista, 32bit, Python 2.6, PIL and pyOpenGL installed). However, when I try to import the wxPython module, I get this: >>> import...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.