【问题标题】:Cannot import C++ Boost.Python class/library in python无法在 python 中导入 C++ Boost.Python 类/库
【发布时间】:2021-09-08 16:36:12
【问题描述】:

我的 C++ 类设置如下:

#include <boost/python.hpp>
#include "Python.h"

namespace something
{
namespace other
{

BOOST_PYTHON_MODULE(classname)
{
    method_call_in_other_file();
}

方法调用在另一个文件中:

namespace something
{
namespace other
{

namespace python = boost::python;

void method_call_in_other_file()
{
    //doing stuff
}

Makefile如下:

X86CPPTARGETSOL += libSOMETHING.cpp.so
libSOMETHING.cpp.so,SRCS = \
      CPP_FILE_ONE.cpp \
      CPP_FILE_TWO.cpp
libSOMETHING.cpp.so,DEPSOL = \
      libpython2.7 \
      libboost_python \
      libOTHER_DEPENDENCIES

该库已正确构建在我的 lib/libSOMETHING.cpp.so 文件夹中。 我从 lib 文件夹所在的同一目录启动 python,然后将 lib/ 附加到路径: sys.path.append('./lib')

现在,当我尝试导入我的 boost_python_module 时(如此处的快速入门所示:https://www.boost.org/doc/libs/1_68_0/libs/python/doc/html/tutorial/index.html#tutorial.quickstart

找不到模块:

>>> import classname
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named classname

我也试过直接导入lib文件:

>>> import libSOMETHING
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named libSOMETHING
>>> import libSOMETHING.cpp
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named libSOMETHING.cpp
>>> import libSOMETHING.cpp.so
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named libSOMETHING.cpp.so

使用help('modules') 列出可用模块也不会显示任何类似于此必需导入的内容。

我在这里错过了什么?

【问题讨论】:

    标签: python c++ boost


    【解决方案1】:

    好吧,所以我找到了答案:*.so 文件的名称需要与BOOST_PYTHON_MODULE(classname) 中的classname 完全相同。符号链接也有效。所以我创建了一个从libSOMETHING.cpp.soclassname.so 的符号链接,现在它可以正常工作了。

    【讨论】:

      猜你喜欢
      • 2018-11-24
      • 2013-07-14
      • 2018-11-30
      • 1970-01-01
      • 1970-01-01
      • 2020-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多