[C++-sig] Importing into embedded Python

Stefan Seefeld seefeld at sympatico.ca
Wed Feb 28 16:31:39 CET 2007


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...



More information about the Cplusplus-sig mailing list