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

Gabriel Genellina gagsl-py at yahoo.com.ar
Sat Feb 10 04:27:31 EST 2007


En Sat, 10 Feb 2007 03:57:05 -0300, Jim Hill <jimhill at swcp.com> escribió:

> 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




More information about the Python-list mailing list