Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

PyImport_ImportModule/embedding: surprising behaviors

Question posted by: David Abrahams (Guest) on March 21st, 2007 12:05 AM

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

--
Dave Abrahams
Boost Consulting
www.boost-consulting.com

Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
Aahz's Avatar
Aahz
Guest
n/a Posts
March 24th, 2007
03:45 PM
#2

Re: PyImport_ImportModule/embedding: surprising behaviors
In article <mailman.5359.1174435375.32031.python-list@python.org>,
David Abrahams <dave@boost-consulting.comwrote:
Quote:
Originally Posted by
>
>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.
--
Aahz (aahz@pythoncraft.com) <* http://www.pythoncraft.com/

"Typing is cheap. Thinking is expensive." --Roy Smith

Alex Martelli's Avatar
Alex Martelli
Guest
n/a Posts
March 24th, 2007
04:45 PM
#3

Re: PyImport_ImportModule/embedding: surprising behaviors
Aahz <aahz@pythoncraft.comwrote:
Quote:
Originally Posted by
In article <mailman.5359.1174435375.32031.python-list@python.org>,
David Abrahams <dave@boost-consulting.comwrote:
Quote:
Originally Posted by

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.


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

Ziga Seilnacht's Avatar
Ziga Seilnacht
Guest
n/a Posts
March 24th, 2007
08:25 PM
#4

Re: PyImport_ImportModule/embedding: surprising behaviors
David Abrahams wrote:
Quote:
Originally Posted by
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
Quote:
Originally Posted by
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



David Abrahams's Avatar
David Abrahams
Guest
n/a Posts
October 25th, 2007
08:55 PM
#5

Re: PyImport_ImportModule/embedding: surprising behaviors

 
Not the answer you were looking for? Post your question . . .
169,970 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

  • Didn't find the answer you were looking for?
    Post Your Question
  • Top Community Contributors