I am using Boost.Python to embed an interpreter in my C++ executable and execute some prewritten scripts. I have got it working so that I can call functions in the python file but the python code I want to use imports external files and these imports fail because 'no module named '. If I run the script directly from python everything works as expected however.
So my question is what is the correct way of importing modules in python scripts that are being run via C++ bindings?
C++ Code:
#include "boost/python.hpp"
int main(int argc, char** argv)
{
try
{
Py_Initialize();
boost::python::object test = boost::python::import("__main__");
boost::python::object testDict = test.attr("__dict__");
boost::python::exec_file("test.py", testDict, testDict);
}
catch(boost::python::error_already_set& e)
{
PyErr_Print();
}
return 0;
}
Python Code:
import ModuleX
So it turns out that my problem is a simple case of the module search path not being set correctly when initialised from within C++.
From the Python Documentation intro:
So what this means is that the module search path is in no way set to point at the current working directory, rather it points at the system python install folder.
The solution for me was to correctly set the module search path to point at the current working directory. To do this you need to initialise python and then extract the sys.path value and add any additional paths. Excuse the use of boost if you're not into that; you should be able to easily see how to substitute any string desired.