sys.path not properly initialized (was: PyImport_ImportModule/embedding: surprising behaviors)

Graham Dumpleton Graham.Dumpleton at gmail.com
Thu Oct 25 18:44:46 EDT 2007


On Oct 26, 6:53 am, David Abrahams <d... at boost-consulting.com> wrote:
> > David Abrahams wrote:
> >> I'm seeing highly surprising (and different!) behaviors of
> >> PyImport_ImportModule on Linux and Windows when used in a program with
> >> python embedding.
>
> >> On Linux, ...
>
> <Linux working OK now>
>
> Unfortunately, nothing you have written below or on the pages you
> reference seems to help in Windows.  Here's a simple program that
> demonstrates:
>
>  import.c
> 1KDownload
>
>   'import site' failed; use -v for traceback
>   Traceback (most recent call last):
>     File "<string>", line 1, in <module>
>   ImportError: __import__ not found
>
> >> I can work around the problem by setting PYTHONPATH to point to the
> >> python library directory:
>
> >>   set PYTHONPATH=c:\Python25\Lib
>
> > This happens because Python calculates the initial import path by
> > looking for an executable file named "python" along PATH.
>
> C:\Python25, which contains python.exe, is in the PATH.  If it's
> really looking for an executable named "python" along PATH, that can
> never succeed on Windows, since (I think) only files ending in .exe,
> .bat, and .cmd are executable there.
>
> > You can
> > change this by calling Py_SetProgramName(filename) before calling
> > Py_Initialize(). This is documented in API reference manual:
>
> Uncomment that line in my program above and you'll see it makes
> absolutely no difference.
>
> >http://docs.python.org/api/embedding.html
>
> > That page also describes a few hooks that you can overwrite to
> > modify the initial search path. They are described in more detail
> > on this page:
>
> >http://docs.python.org/api/initialization.html
>
> The only thing mentioned there that seems to have any effect at all is
>
>   set PYTHONHOME=C:\Python25
>
> and even then, it only eliminates the first line of the error, which
> then reads:
>
>   Traceback (most recent call last):
>     File "<string>", line 1, in <module>
>   ImportError: __import__ not found
>
> My only known workaround is to set PYTHONPATH.  This just doesn't seem
> right; isn't anyone doing embedding under Windows?

The problem with not finding site.py on Windows will often come up
when you have gone through a cycle of installing/removing different
versions of Python and the registry entries get mucked. Also can be a
problem where Python was not installed as administrator originally and
now trying to run stuff as user different to what Python was installed
as. A lot of the time problems go away by deinstalling Python and
reinstalling as administrator. Also watch out for where you have some
third party application which provides its own version of Python and
that version or its DLL is being found first in your PATH.

Graham





More information about the Python-list mailing list