Problem with sys.path when embedding Python3 in C

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue Mar 23 08:07:47 CET 2010


En Mon, 22 Mar 2010 18:19:49 -0300, Krister Svanlund  
<krister.svanlund at gmail.com> escribió:

> Hi, I've recently begun experimenting with embedding python and i got
> a small problem.
>
> The following line here is the ugly-hack I had to do to make it work,
> nothing else I know of makes it possible to import modules from
> startup directory. So my question is: Is there a prettier way to do
> this?

The startup directory is not included in the module search path - neither  
in your embedded version, nor in the standard interpreter (it's only  
included when running in interactive mode).

>> PyRun_SimpleString("import sys\nsys.path.append(\"\")");

If you really want the current directory in sys.path, use the getcwd  
function to obtain it. But make sure this is what you want - the directory  
containing the executable might be a better choice (at least more  
predictable).
Note that Python already provides lots of ways to add directories to  
sys.path (the default search path (see site.py), per-user site directories  
(see PEP370), .pth files, the PYTHONPATH and PYTHONHOME environment  
variables, the Windows registry, other ways I forgot...) So I'd ask why do  
you want to add a non-standard one.

In C code, you can alter the initial search path by setting   
Py_SetProgramName and Py_SetPythonHome. And you may even completely  
replace getpathp.c source file with your own.

-- 
Gabriel Genellina




More information about the Python-list mailing list