Problem with sys.path when embedding Python3 in C

Krister Svanlund krister.svanlund at gmail.com
Tue Mar 23 05:23:53 EDT 2010


On Tue, Mar 23, 2010 at 8:07 AM, Gabriel Genellina
<gagsl-py2 at yahoo.com.ar> wrote:
> 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
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

The thing is that I want the application to be able to import modules
I've written, but yeah, the applications directory is what I want
rather than the cwd. I have tried Py_SetProgramName but haven't gotten
it to work or cause any change at all to the import behaviour. Could
you possibly provide som sort of example?



More information about the Python-list mailing list