473,624 Members | 2,475 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PyImport_Import Module/embedding: surprising behaviors


I'm seeing highly surprising (and different!) behaviors of
PyImport_Import Module 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:\P ython25\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 5680
In article <ma************ *************** ************@py thon.org>,
David Abrahams <da**@boost-consulting.comw rote:
>
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**@pythoncra ft.com) <* http://www.pythoncraft.com/

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

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_Import Module 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:\P ython25\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_SetProgramNa me(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
2775
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 working with Python for a year or more now, but only doing simple extending in C/C++. I'm now attempting some embedding and several questions have come to mind. BTW - I'm running Windows 2000 with Python23 and VisualC++ developers
0
1326
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 behaviors). Can anyone clarify: - if Python correctly supports multiple sub-interpreters (Py_NewInterpreter) ?
0
1419
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 behaviors). Can anyone clarify:
0
1627
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 absolute. They are inside of another parent div container with position set relative so that as I make the objects visible, I can also set their position to relative so they redraw the container size. The parent div is inside of a table cell...
4
1678
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 behaviors). Can anyone clarify:
45
2882
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 to check. What I expected was that there would be minor code bloat and some speed improvement when using templates. However... I wrote a basic list container (using templates), and a list container (using virtual derived classes). I also tried...
2
3242
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 available in the python C API. More specifically I am trying to use Pyimport_ImportModule() to import sys, and it doesn't work. I have also tried using PyString_FromString() to convert the the string "sys" to a Python object to then use...
1
2762
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 module, I am importing only the sys module. Py_SetProgramName("/usr/bin/python"); Py_Initialize(); char* argv = { "python","-v",""}; PySys_SetArgv(2,argv);
1
1629
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 deadlocks the process, is there a need for me to acquire locks inside of my stdout catching function before calling that import?
0
8246
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8179
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8685
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8490
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7174
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6112
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4184
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2612
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.