Connecting Tech Pros Worldwide Help | Site Map

PyImport_ImportModule/embedding: surprising behaviors

 
LinkBack Thread Tools Search this Thread
  #1  
Old March 21st, 2007, 12:05 AM
David Abrahams
Guest
 
Posts: n/a
Default PyImport_ImportModule/embedding: surprising behaviors


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


  #2  
Old March 24th, 2007, 03:45 PM
Aahz
Guest
 
Posts: n/a
Default Re: PyImport_ImportModule/embedding: surprising behaviors

In article <mailman.5359.1174435375.32031.python-list@python.org>,
David Abrahams <dave@boost-consulting.comwrote:
Quote:
>
>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
  #3  
Old March 24th, 2007, 04:45 PM
Alex Martelli
Guest
 
Posts: n/a
Default Re: PyImport_ImportModule/embedding: surprising behaviors

Aahz <aahz@pythoncraft.comwrote:
Quote:
In article <mailman.5359.1174435375.32031.python-list@python.org>,
David Abrahams <dave@boost-consulting.comwrote:
Quote:

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
  #4  
Old March 24th, 2007, 08:25 PM
Ziga Seilnacht
Guest
 
Posts: n/a
Default Re: PyImport_ImportModule/embedding: surprising behaviors

David Abrahams wrote:
Quote:
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:
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


  #5  
Old October 25th, 2007, 08:55 PM
David Abrahams
Guest
 
Posts: n/a
Default [Windows embedding] sys.path not properly initialized (was:

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.