I have a piece of software that embeds python using libboost-python. It creates a python module in C++ runtime, and then imports it from python code.
For python2.7 this works well. See test_module.py and test27.cpp attached. On Debian Stretch it can be build using command g++ test27.cpp -I /usr/include/x86_64-linux-gnu/python2.7 -I /usr/include/ python2.7 -lboost_python -lboost_system -lpython2.7 As I've said It works as expected. Python code imports _hello_provider module that have been created in C++ code, and uses provide_hello() for printing. I've tried to port it to python 3.x, and did not succeed. I managed to write test3x.cpp, it build well on Debian Buster using command g++ test3x.cpp -I /usr/include/x86_64-linux-gnu/python3.9 -I /usr/include/ python3.9 -lpython3.9 -lboost_python39 -lboost_system but it fails when I try to run it. Traceback (most recent call last): File "/usr/lib/python3.9/imp.py", line 169, in load_source module = _exec(spec, sys.modules[name]) File "<frozen importlib._bootstrap>", line 613, in _exec File "<frozen importlib._bootstrap_external>", line 790, in exec_module File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed File "test_module.py", line 1, in <module> import _hello_provider ModuleNotFoundError: No module named '_hello_provider' for some reason "native" python module does not see _hello_provider that have been created in runtime. I guess I am doing something wrong, but I am no expert in python and python embedding, and I did not managed to find any good example in the internet. So I guess I need some help here. P.S. If remove "import" related part from examples, it also works well, so the only task here is to make in properly visible... -- Nikolay Shaplov aka Nataraj Fuzzing Engineer at Postgres Professional Matrix IM: @dhyan:nataraj.su
#include <boost/python.hpp> std::string provide_hello() { return "hello world provided"; } BOOST_PYTHON_MODULE(_hello_provider) { using namespace boost::python; def("provide_hello", &provide_hello); } int main() { Py_InitializeEx(0); try { boost::python::object modImp = boost::python::import("imp"); PyInit__hello_provider(); PyImport_AddModule("test_module"); modImp.attr("load_source")("test_module", "test_module.py"); boost::python::exec("print(test_var)", boost::python::import("test_module").attr("__dict__")); boost::python::exec("print(hello_static())", boost::python::import("test_module").attr("__dict__")); boost::python::exec("print(hello_provided())", boost::python::import("test_module").attr("__dict__")); } catch (const boost::python::error_already_set&) { PyErr_Print(); } Py_Finalize(); }
#include <boost/python.hpp> std::string provide_hello() { return "hello world provided"; } BOOST_PYTHON_MODULE(_hello_provider) { using namespace boost::python; def("provide_hello", &provide_hello); } int main() { Py_InitializeEx(0); try { boost::python::object modImp = boost::python::import("imp"); init_hello_provider(); PyImport_AddModule("test_module"); modImp.attr("load_source")("test_module", "test_module.py"); boost::python::exec("print(test_var)", boost::python::import("test_module").attr("__dict__")); boost::python::exec("print(hello_static())", boost::python::import("test_module").attr("__dict__")); boost::python::exec("print(hello_provided())", boost::python::import("test_module").attr("__dict__")); } catch (const boost::python::error_already_set&) { PyErr_Print(); } Py_Finalize(); }
import _hello_provider test_var = 42 def test_out(expr): print(expr) def hello_static(): return "Hello world static" def hello_provided(): return _hello_provider.provide_hello() #test_out('lalala')
signature.asc
Description: This is a digitally signed message part.
_______________________________________________ Cplusplus-sig mailing list Cplusplus-sig@python.org https://mail.python.org/mailman/listinfo/cplusplus-sig