Hi!
I want to create a python module in embedded python from string (The
module script is stored in a database).
My code looks like this:
command.py:
class Command:
def execute(self):
return "foo"
main.cpp:
#include <iostream>
#include <fstream>
#include <boost/python.hpp>
int main(int argc, char **argv)
{
Py_Initialize();
try
{
// read the Command class
std::stringstream buffer;
std::ifstream ifile("command.py");
buffer << ifile.rdbuf();
ifile.close();
boost::python::str command_class(buffer.str());
// create module
boost::python::object py_module_new =
boost::python::import("new");
boost::python::object module =
py_module_new.attr("module")("testmodule");
boost::python::object module_namespace =
module.attr("__dict__");
// add Command class to the module
boost::python::exec(command_class, module_namespace);
// test Command
boost::python::object command = module.attr("Command")();
boost::python::object r = command.attr("execute")();
std::string value = boost::python::extract<std::string>(r);
std::cout << value << std::endl;
}
catch(...)
{
PyErr_Print();
PyErr_Clear();
}
Py_Finalize();
return 0;
}
It works as expected.
But when I add and import command to the command.py file, it fails:
command.py:
import math
class Command:
def execute(self):
return "foo"
The error is:
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: __import__ not found
What am I missing?
Thanks in advance:
Imre Horvath
_______________________________________________
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig