I am trying to create Python extension module that combine both C++ and Python code. So far I was able to run Python code fine but I can’t find a way for my Python object to appear as part of my module import. What am I doing wrong?
Here the example code for my C++ module (I am omitting C++/boost::python code that bind C++ function and classes for clarity):
#include <boost/python.hpp>
BOOST_PYTHON_MODULE( my_module ) {
<... some boost::python code to bind C++ classes/functions ...>
boost::python::object main_module = boost::python::import("__main__");
boost::python::object main_namespace = main_module.attr("__dict__”);
boost::python::exec("aaa = 'ABC’\n”, main_namespace, main_namespace);
}
And later in Python if I try:
import my_module
print my_module.aaa <— error!
(I know that I can separate python files and C++ dynamic libs in to its own files and it will all work, but i really need my extension module to be just one file for other technical reasons)