【问题标题】:Python complaining that a SWIG module doesn't existsPython 抱怨 SWIG 模块不存在
【发布时间】:2013-01-30 17:06:06
【问题描述】:

按照网络上的不同教程,我尝试使用 SWIG 在 python 中制作 c++ 类的包装器。

我的班级是这样的:

/*file libraryInstance.h*/
struct LibraryInstance
{
    void init();
    void terminate();
private:
    std::shared_ptr<AnObject> m_spAnObject;
};

为了展示 python,我制作了这个 .i 文件:

%module LibraryInstance
%{
#include "libraryInstance.h"
%}
%include "libraryInstance.h"

那么我已经执行了命令swig -c++ -python -o ./src/libraryInstance_wrap.cpp ./src/libraryInstance.i

没有任何输出错误,swig 生成了两个文件,libraryInstance_wrap.cppLibraryInstance.py

然后我编译 c++ 文件,包括libraryInstance_wrap.cpp。所有编译都很好,我得到了我的库 .so 文件。

当我查看生成的LibraryInstance.py swig 时,我可以清楚地看到class LibraryInstance

cf. entire generated python wrapper here.

但是当我启动命令 python LibraryInstance.py 时,在与我的 .so 相同的目录中,我看到了这个错误输出:

Traceback (most recent call last):
  File "LibraryInstance.py", line 26, in <module>
    _LibraryInstance = swig_import_helper()
  File "LibraryInstance.py", line 18, in swig_import_helper
    import _LibraryInstance
ImportError: No module named _LibraryInstance

当我查看LibraryInstance.py的代码时,它看起来好像抛出了异常ImportError,python找不到模块。 (第 18 行)。

知道我应该怎么做才能纠正这个问题吗?

【问题讨论】:

    标签: c++ python linux swig


    【解决方案1】:

    在 SWIG 文档中,paragraph 31.2.2 声明库 .so 的名称应为 _NameOfTheModule.so

    所以我将我的库重命名为 _LibraryInstance.so,而不是 LibraryInstance.so... 现在我的模块加载正常。

    【讨论】:

    • FWIW,该库必须在 Windows 下命名为 _NameOfTheModule.pyd(自 Python 2.5 起)
    猜你喜欢
    • 2019-01-01
    • 2016-07-08
    • 2021-12-13
    • 2016-11-03
    • 1970-01-01
    • 1970-01-01
    • 2016-09-18
    • 2020-05-15
    • 1970-01-01
    相关资源
    最近更新 更多