Why or how does the file __init__.py
cause the python interpreter to search
subdirectories for a module -- and why does the interpreter not honor this
convention when invoked from C++?
Here's what I know:
Using strace on my program, I can see that the same python2.5 interpreter is being executed for both the interactive case and the C++ invocation.
In both cases, the PYTHONPATH directs the
search for the imported module in question (matplotlib). This appears as a
series of open() calls, starting from the current working directory and
extending to the PYTHONPATH (here, /opt/epd/lib/python2.5/site-packages
), and
lastly into the subdirectories, in the working case.
The full disclosure is that I am using the "Enthought" distribution and had to
place the __init__.py
file in the site-packages
directory and put the
site-packages
directory in the PYTHONPATH to create the working case.
The code is below. It seems that I may need to make a call to configure the
python interpreter to look for __init__
and/or recurse, in order to find the
requested packages. IF SO, HOW??
PyObject* main_module, * global_dict, * expression, *args, *spec;
Py_Initialize ();
char* script = "abc.py";
PySys_SetArgv(1, &script);
//Open the file containing the python modules we want to run
FILE* file_1 = fopen("abc.py", "r");
if (file_1 == 0) fprintf(stdout, "ERROR: File not opened");
//Loads the python file into the interpreter
PyRun_SimpleFile(file_1, "abc.py");
//Creates a python object that contains references to the functions and classes in abc.py
main_module = PyImport_AddModule("__main__");
global_dict = PyModule_GetDict(main_module);
expression = PyDict_GetItemString(global_dict, "view_gui");
spec = PyObject_CallObject(expression, NULL);
PyObject_CallMethod(spec, "shutdown", NULL);
Py_Finalize();
return NULL;
When the python script is invoked from C++, the search seems to
stop when the file /opt/epd/lib/python2.5/site-packages/matplotlib
(or it's
variant, matplotlib.so, etc) are not found.
Note that I can augment the PYTHONPATH to include the exact location of
matplotlib (and other needed packages) to get farther; however, I cannot seem
to include a path to import matplotlib.cbook
.