如何使用boost/python从C ++导入.py文件中的类? [英] How do I import a class in a .py file from C++ using boost/python?

查看:72
本文介绍了如何使用boost/python从C ++导入.py文件中的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看来我已经弄乱了第二篇文章的解决方案,但是在纠正它之后仍然给我一个错误.

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

目录如下:

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

从bar.py中,我可以像这样从foo.py中导入一些东西:

From bar.py I can just import something from foo.py like this:

from ..utilities.foo import Foo

但是,我不确定如何从myclass.cpp导入它.我已经尝试过

However, from myclass.cpp I'm not sure how to import it. I've tried

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

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");
    }
}

我也尝试使用 sys os ,但是它仍然给我一个错误(从对

I have also tried using sys and os however it still gave me an error (from an answer to this post ):

    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;
    }

错误消息:

ModuleNotFoundError: No module named '.'

我应该如何导入模块?

谢谢

推荐答案

我想出了一种解决方法:由于Python模块也是PyObject,因此我可以在Python中加载该模块并将其作为变量传递给C ++.这是代码:

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. Here's the code:

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;
}

在python中(来自此答案):

In python (from this answer) :

from .utilities import foo as foo_module
set_globals(foo_module)

输出:

1
1
1

这篇关于如何使用boost/python从C ++导入.py文件中的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆