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

David Abrahams dave at boost-consulting.com
Thu Oct 25 16:53:04 EDT 2007


on Sat Mar 24 2007, "Ziga Seilnacht" <ziga.seilnacht-AT-gmail.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:

-------------- next part --------------
A non-text attachment was scrubbed...
Name: import.c
Type: text/x-csrc
Size: 302 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20071025/18df94c6/attachment.c>
-------------- next part --------------

The output is:

  '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?

-- 
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com


More information about the Python-list mailing list