473,326 Members | 2,655 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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

Mar 21 '07 #1
4 5649
In article <ma***************************************@python. org>,
David Abrahams <da**@boost-consulting.comwrote:
>
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 (aa**@pythoncraft.com) <* http://www.pythoncraft.com/

"Typing is cheap. Thinking is expensive." --Roy Smith
Mar 24 '07 #2
Aahz <aa**@pythoncraft.comwrote:
In article <ma***************************************@python. org>,
David Abrahams <da**@boost-consulting.comwrote:

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
Mar 24 '07 #3
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, 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
Mar 24 '07 #4
Oct 25 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Alicia Haumann | last post by:
I accidentally sent this to webmaster@python.org, so this could be a duplicate if "webmaster" forwards it to this list. :{ Hi, there. Thanks for any help that can be offered. I've been...
0
by: adsheehan | last post by:
Hi, I am embedding Python into a multi-threaded C++ application runnig on Solaris and need urgent clarification on the embedding architecture and its correct usage (as I am experience weird...
0
by: adsheehan | last post by:
Hi, I am embedding Python into a multi-threaded C++ application runnig on Solaris and need urgent clarification on the embedding architecture and its correct usage (as I am experience weird...
0
by: Catherine Lynn Wood | last post by:
I have a page that I just developed using a combination of stylesheets and div layers. It uses a 'tab' style system placing four div layers in the same space with visibility 'hidden' and position...
4
by: adsheehan | last post by:
Hi, I am embedding Python into a multi-threaded C++ application running on Solaris and need urgent clarification on the embedding architecture and its correct usage (as I am experience weird...
45
by: charles.lobo | last post by:
Hi, I have recently begun using templates in C++ and have found it to be quite useful. However, hearing stories of code bloat and assorted problems I decided to write a couple of small programs...
2
Bulldog
by: Bulldog | last post by:
I am new to this board and I am using Python 2.2.1 on Windows XP SP2. I am working on embedding some python functionality in a C++ win32 DLL, and I am having issues using the PyImport_ functions...
1
by: Borse, Ganesh | last post by:
Hi, Can you please guide me for the following problem? The call to "PyImport_ImportModule("cStringIO");" is failing with an error of "undefined symbol: PyObject_SelfIter". Before importing this...
1
by: jms | last post by:
Hey all, I have an embedded Python shell and everything works fine, however, in my stdout catcher (in C to grab tracebacks) for some reason when I do a : PyImport_ImportModule( "sys" ) It...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.