PyImport_ImportModule/embedding: surprising behaviors

D

David Abrahams

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, when attempting to import a module xxx that's in the current
directory, I get

ImportError: No module named xxx

I can work around the problem by setting

PYTHONPATH=.

On Windows, I get:

'import site' failed; use -v for traceback

I can work around the problem by setting PYTHONPATH to point to the
python library directory:

set PYTHONPATH=c:\Python25\Lib

I was under the impression that both the current directory *and* the
python library directory were already, automatically, in sys.path, so
I'm really surprised to see this. Am I doing something wrong, or is
this simply the expected behavior (and if so, where is it documented)?
 
A

Aahz

I was under the impression that both the current directory *and* the
python library directory were already, automatically, in sys.path, so
I'm really surprised to see this. Am I doing something wrong, or is
this simply the expected behavior (and if so, where is it documented)?

IIRC (without bother to test), there has been some change in the
definition of "current directory" -- it used to be the actual current
directory of os.getcwd(), but since changed to the startup directory.
 
A

Alex Martelli

Aahz said:
IIRC (without bother to test), there has been some change in the
definition of "current directory" -- it used to be the actual current
directory of os.getcwd(), but since changed to the startup directory.

In 2.3 and later, at least (sorry, no 2.2 and earlier around to check),
site.py makes every directory along sys.path an absolute path at Python
startup. This _should_ probably be documented at
<http://docs.python.org/lib/module-site.html>, but it doesn't appear to
be clearly stated there (the page only speaks of site's job of
"appending site specific paths", and not of the other jobs it also
performs, such as normalizing sys.path by turning all paths into
absolute ones and removing duplicates).


Alex
 
Z

Ziga Seilnacht

David said:
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, when attempting to import a module xxx that's in the current
directory, I get

ImportError: No module named xxx

I can work around the problem by setting

PYTHONPATH=.

Python puts the current directory in sys.path only if it can't
determine the directory of the main script. There was a bug on
Windows that always added current directory to sys.path, but it
was fixed in Python 2.5. This is documented in the library
reference:

http://docs.python.org/lib/module-sys.html#l2h-5149
On Windows, I get:

'import site' failed; use -v for traceback

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. You can
change this by calling Py_SetProgramName(filename) before calling
Py_Initialize(). This is documented in API reference manual:

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

HTH,
Ziga
 
G

Graham Dumpleton

<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):



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.


Uncomment that line in my program above and you'll see it makes
absolutely no difference.




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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top