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

Jim Hill jimhill at swcp.com
Sat Feb 10 02:01:25 EST 2007


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('LINKFORSHARED')
>
>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."



More information about the Python-list mailing list