Python抱怨SWIG模块不存在
我在网上跟着不同的教程,尝试用SWIG把一个C++类封装成Python类。
我的类大概是这样的:
/*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.cpp
和LibraryInstance.py
。
接着我编译C++文件,包括libraryInstance_wrap.cpp
。编译都很顺利,最后得到了我的库文件.so。
当我查看SWIG生成的LibraryInstance.py
时,可以清楚地看到class LibraryInstance
:
但是当我在和我的.so文件同一个目录下运行命令python LibraryInstance.py
时,看到这个错误输出:
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行)
请问我该怎么做才能解决这个问题呢?