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:

#include "Python.h"

int main()
{
    // uncomment to test the effect of Py_SetProgramName
    // Py_SetProgramName("c:\\Python25\\python.exe");
    
    Py_Initialize();
    if (!PyRun_String("import sys\nimport os", Py_file_input, Py_None, Py_None))
    {
        PyErr_Print();
    }
}
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
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to