[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Re: Problem with sys.path when embedding Python3 in C

Gabriel Genellina

3/23/2010 7:08:00 AM

En Mon, 22 Mar 2010 18:19:49 -0300, Krister Svanlund
<krister.svanlund@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