[C++-sig] Python API, Initialization, Path and classes

nojhan nojhan at free.fr
Sat May 13 20:38:56 CEST 2006

I'm trying to embbed the python interpreter as a class attribute,
initializing it in the constructor, and finalizing in destructor.

The code is rather simple:

// base_path is an attribute of the class, 
// initialized with argv[0] at the instanciation
clog << "Python base program name asked: " << base_path <<
endl; Py_SetProgramName( base_path );
clog << "Python isInitialized ? " << ( Py_IsInitialized()?"yes":"no" ) <<
endl; clog << "Python base program name set:" << Py_GetProgramName() <<
endl; clog << "Python path: " << Py_GetPath() << endl;

But it produce a rather strange output :

Python base program name asked: /home/nojhan/travail/openMetaheuristic/source/ometah/ometah
Python isInitialized ? yes
Python base program name set:/home/nojhan/travail/openMetaheuristic/source/ometah/ometah
Python path: /usr/lib/python24.zip:/usr/lib/python2.4/:/usr/lib/python2.4/plat-linux2:/usr/lib/python2.4/lib-tk:/usr/lib/python2.4/lib-dynload

The code ends in an ImportError if I try to import a module in the current

Note that the python C API reference manual precise that
Py_Initialize() "initializes the module search path (sys.path)" [1]. 

I'm searching for a way to force the python interpreter considering the
current path when searching for available modules.

[1] http://docs.python.org/api/initialization.html


More information about the Cplusplus-sig mailing list