Ok, a couple of things missing:
1) You must tell Python about your module before you can use it. This can be done in a number of ways, but I find PyImport_AppendInittab easiest to use.
2) You still have to call PyErr_Print() to print the error to stderr.
3) Not a error as such, but you want to call PyErr_Clear() after PyErr_Print, unless you wont be using Python again.
int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); // Test python script QString python_code("# -*- coding: iso-8859-15 -*-\n" "result )= 5 ** 2\n" "OUT = {'language' : 'German', 'name' : 'Max Mustermann', 'age' : 28}"); PyImport_AppendInittab( "module_name", initFoo ); // <-- Note that initFoo is auto generated Py_Initialize(); Foo foo; bp::import("sys").attr("stderr") = foo; try { PyRun_SimpleString(python_code.toStdString().c_str());//"result = 5 ** 2"); bp::object module(bp::handle<>(bp::borrowed(PyImport_AddModule("__main__")))); bp::object dictionary = module.attr("__dict__"); bp::object result = dictionary["result"]; //double result_value = extract<double>(result); { bp::extract<int> x(result); if (x.check()) { int v = x(); qDebug() << "Extract Type: Integer = " << QString("%1").arg(v); } }{ bp::extract<double> x(result); if (x.check()) { double v = x(); qDebug() << "Extract Type: Double = " << QString("%1").arg(v); } }{ bp::extract<bool> x(result); if (x.check()) { bool v = x(); qDebug() << "Extract Type: Boolean = " << QString("%1").arg(v); } }{ bp::extract<long> x(result); if (x.check()) { long v = x(); qDebug() << "Extract Type: long = " << QString("%1").arg(v); } }{ bp::extract<std::string> x(result); if (x.check()) { std::string v = x(); qDebug() << "Extract Type: std::string = " << QString("%1").arg(v.c_str()); } } } catch(...) { PyErr_Print(); // <-- PyErr_Clear(); // <-- cerr << "Out1: " << foo.m_s << endl; } cerr << "Out2: " << foo.m_s << endl; Py_Finalize(); return 0;}
Again, I can't test the code frem here, but I believe it should work.
When doing boost::python bindings, these three pages are your friends:
Python/C API Reference Manualboost::python tutorialboost::pytho n reference manual