Connecting Tech Pros Worldwide Help | Site Map

PyImport_ImportModule/embedding: surprising behaviors

  #1  
Old March 21st, 2007, 01:05 AM
David Abrahams
Guest
 
Posts: n/a

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, 04:45 PM
Aahz
Guest
 
Posts: n/a

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, 05:45 PM
Alex Martelli
Guest
 
Posts: n/a

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, 09:25 PM
Ziga Seilnacht
Guest
 
Posts: n/a

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, 09:55 PM
David Abrahams
Guest
 
Posts: n/a

re: PyImport_ImportModule/embedding: surprising behaviors


Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Question: Threading and embedding python in an application David Harrison answers 3 July 19th, 2005 02:07 AM