Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* pytest.cpp */
- /* build: g++ -std=c++1y -fPIC -I/usr/include/python3.4 -lboost_python -lpython3.4m pytest.cpp */
- /* bould: g++ -std=c++1y -fPIC -I/usr/include/python3.4 -lboost_python-py34 -lpython3.4m pytest.cpp */
- #include <string>
- #include <iostream>
- #include <boost/python.hpp>
- using namespace boost::python;
- int main() {
- try {
- Py_Initialize();
- object main_module = import("__main__");
- object main_namespace = main_module.attr("__dict__");
- /* Тут import os нормально отработал */
- object tstObj = exec("print('This is Python code...')\n"
- "import os\n"
- "print(os.getcwd())\n",
- main_namespace);
- /* А тут import mymodule не импортирует */
- object testTwo = exec_file("test.py", main_namespace);
- /* Тоже не катит */
- object mymodObj = import("mymodule");
- /* Note that at this time you must not call Py_Finalize() to stop the interpreter. This may be fixed in a future version of boost.python. */
- /* http://www.boost.org/doc/libs/master/libs/python/doc/html/tutorial/tutorial/embedding.html */
- /* Py_Finalize(); */
- } catch(error_already_set const &) {
- if (PyErr_ExceptionMatches(PyExc_ZeroDivisionError)) {
- /* handle ZeroDivisionError specially */
- } else {
- /* print all other errors to stderr */
- PyErr_Print();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment