Importing into embedded Python
I'm having problems importing modules containing Boost.Python produced bindings into an embedded Python interpreter. The following compiles into a module that loads fine into a standalone Python: testing.hh: ------------------ class A { }; ------------------ testing.cpp ------------------ #include <boost/python.hpp> #include "testing.hh" using namespace boost::python; BOOST_PYTHON_MODULE(testing) { class_<A>("A", init<>()) ; } ------------------- However, I get problems when I try to import the module into an embedded Python interpreter as follows: Py_Initialize(); PyRun_SimpleString("print 'Running Python'\n" "import testing\n" "print 'imported testing'\n" "a = testing.A() \n" "print 'a=', a \n"); The program outputs: Running Python imported testing Traceback (most recent call last): File "<string>", line 4, in ? AttributeError: 'module' object has no attribute 'A' I've googled around and found several pages describing similar problems, but so far no solutions. My setup is OS X 10.4, Python 2.4.4 and GCC 4.1.1 if that is of any help. -- Pertti
Pertti Kellomäki wrote:
I'm having problems importing modules containing Boost.Python produced bindings into an embedded Python interpreter.
Incidently, I ran into the same problem yesterday. I solved it by calling inittesting() after Py_Initialize(). HTH, m Send instant messages to your online friends http://au.messenger.yahoo.com
Pertti Kellomäki wrote:
I'm having problems importing modules containing Boost.Python produced bindings into an embedded Python interpreter.
The following compiles into a module that loads fine into a standalone Python:
testing.hh: ------------------ class A { }; ------------------
testing.cpp ------------------ #include <boost/python.hpp> #include "testing.hh"
using namespace boost::python;
BOOST_PYTHON_MODULE(testing) { class_<A>("A", init<>()) ; } -------------------
However, I get problems when I try to import the module into an embedded Python interpreter as follows:
Py_Initialize(); PyRun_SimpleString("print 'Running Python'\n" "import testing\n" "print 'imported testing'\n" "a = testing.A() \n" "print 'a=', a \n");
You seem to be missing a call to PyImport_AppendInittab("testing", inittesting); before the call to PyRun_SimpleString. (Also note that the soon-to-be boost 1.34 has an enhanced API for the above, boost::python::exec() and boost::python::exec_file().) HTH, Stefan -- ...ich hab' noch einen Koffer in Berlin...
Stefan Seefeld wrote:
You seem to be missing a call to
PyImport_AppendInittab("testing", inittesting);
before the call to PyRun_SimpleString.
Thanks, that indeed allows me to use stuff in the testing module. It is not quite what I was hoping for, as the Python code will come from a user-written file, and ideally I would like the Python code to be able to load arbitrary modules. However, the set of Boost.Python produced modules is fairly small, so in a pinch I can simply import all of them using PyImport_AppendInittab. A tad too brute force to my taste, but I can live with it.
(Also note that the soon-to-be boost 1.34 has an enhanced API for the above, boost::python::exec() and boost::python::exec_file().)
I'm looking forward for 1.34 to come out officially. -- Pertti
participants (3)
-
Martin Wille -
Pertti Kellomäki -
Stefan Seefeld