简体   繁体   English

如何使用 boost/python 从 C++ 的 .py 文件中导入 class?

[英]How do I import a class in a .py file from C++ using boost/python?

EDIT: Looks like I've messed up the solution from the second post, but it's still giving me an error after I corrected it.编辑:看起来我已经把第二篇文章的解决方案搞砸了,但在我更正之后它仍然给我一个错误。

Edit: I tried importing using a full path but it gave me a relative import error.编辑:我尝试使用完整路径导入,但它给了我一个相对导入错误。 I made another post about it here我在这里发表了另一篇关于它的文章

the directories looks like this:目录如下所示:

project
   |__ utilities
   |      |__ foo.py
   |
   |__ boost_extensions
   |      |__ myclass.cpp
   |      |__ myclass.so
   |
   |__ someotherstuff
   |      |__ bar.py      
   |
   |__ __main__.py

From bar.py I can just import something from foo.py like this:从 bar.py 我可以像这样从 foo.py 导入一些东西:

from ..utilities.foo import Foo

However, from myclass.cpp I'm not sure how to import it.但是,从 myclass.cpp 我不确定如何导入它。 I've tried我试过了

boost::python::object mod = boost::python::import("..utilities.foo");

and

boost::python::object mod = boost::python::import("../utilities/foo.py");

both gave me an error module not found error:两者都给了我一个错误模块未找到错误:

ModuleNotFoundError: No module named '.'

I've also seen this post and tried the accepted answer but it did not work (same error as before):我也看过这篇文章并尝试了接受的答案,但它没有用(与以前相同的错误):

boost::python::object mod;
void set_global(){
    try{
        setenv("PYTHONPATH", ".", 1);
        Py_Initialize();
        mod = boost::python::import("..utilities.foo");
    }
}

I have also tried using sys and os however it still gave me an error (from an answer to this post ):我也尝试过使用sysos但是它仍然给了我一个错误(来自对这篇文章的回答):

    try{
        setenv("PYTHONPATH", ".", 1);
        Py_Initialize();
        boost::python::object sys = import("sys");
        boost::python::object os = import("os");
        // sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'Common'))
        // os.path.dirname(__file__)
        boost::python::object arg1 = os.attr("path").attr("dirname")("__file__");
        // os.path.join(arg1, '..', 'Common')
        boost::python::object arg2 = os.attr("path").attr("join")(arg1, "..", "Common");
        // sys.path.append(arg2)
        sys.attr("path").attr("append")(arg2);
        mod = boost::python::import("..utilities.foo");
    } catch(int e){
        cout << "import failed" << endl;
    }

error message:错误信息:

ModuleNotFoundError: No module named '.'

How am I supposed to import the module?我应该如何导入模块?

Thanks谢谢

I figured out a workaround: since Python modules are also PyObjects, I can load the module in Python and pass it to C++ as a variable.我想出了一个解决方法:由于 Python 模块也是 PyObjects,我可以在 Python 中加载模块并将其作为变量传递给 C++。 Here's the code:这是代码:

The C++ file: C++ 文件:

boost::python::object mod;
void set_global(boost::python::object foo_module){
    mod = foo_module;
    // this is for testing
    boost::python::object test = mod.attr("Foo")(new A(1,1,1), NULL, 12);
    A* a = extract<A*>(test.attr("_A")) ;
    cout << a->_x << endl << a->_y << endl << a->_z << endl;
}

In python (from this answer ):在 python 中(来自这个答案):

from .utilities import foo as foo_module
set_globals(foo_module)

output: output:

1
1
1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM