[英]SWIG + setup.py: ImportError: dynamic module does not define init function (init_foo)
I am trying to wrap a function foo in test.cpp
with swig. 我正在尝试使用swig将功能foo包装在
test.cpp
。 I have a header foo.h
which contains the declaration of the function foo. 我有一个头文件
foo.h
,其中包含函数foo的声明。 test.cpp
is dependent upon a external header ex.h
and shared object file libex.so
located in /usr/lib64
test.cpp
依赖于外部头文件ex.h
和/usr/lib64
共享对象文件libex.so
I followed the blog post from here . 我从这里关注博客文章 。
I am able to build the module with python setup.py build_ext --inplace
. 我可以使用
python setup.py build_ext --inplace
构建模块。 However when I try to import it I get the following error and I am not sure what I am missing as most others questions with this error do not use a setup.py
file. 但是,当我尝试导入它时,出现以下错误,我不确定我缺少什么,因为大多数其他与此错误有关的问题都不使用
setup.py
文件。 Below is an example of what I currently have. 以下是我目前拥有的一个示例。
The Error on importing _foo: 导入_foo时出现错误:
>>> import _foo
ImportError: dynamic module does not define init function (init_foo)
test.i 测试
%module foo
%{
#pragma warning(disable : 4996)
#define SWIG_FILE_WITH_INIT
#include "test.h"
%}
%include <std_vector.i>
%include <std_string.i>
%include "test.h"
test.cpp 测试文件
#include "ex.h"
void foo(int i){
return;
};
test.h 测试
#include "ex.h"
void foo(int i);
setup.py setup.py
try:
from setuptools.command.build_ext import build_ext
from setuptools import setup, Extension, Command
except:
from distutils.command.build_ext import build_ext
from distutils import setup, Extension, Command
foo_module = Extension('_foo',
sources=['foo.i' , 'foo.cpp'],
swig_opts=['-c++'],
library_dirs=['/usr/lib64'],
libraries=['ex'],
include_dirs = ['/usr/include'],
extra_compile_args = ['-DNDEBUG', '-DUNIX', '-D__UNIX', '-m64', '-fPIC', '-O2', '-w', '-fmessage-length=0'])
setup(name='mymodule',
ext_modules=[foo_module],
py_modules=["foo"],
)
Is looks like there is some inconsistency in the use of foo
and _foo
, as the wrap file is generated compiled and linked in. 看起来
foo
和_foo
的使用存在一些不一致,因为包装文件是在编译和链接时生成的。
Try changing the module name in test.i
from 尝试从以下位置更改
test.i
的模块名称
%module foo
to 至
%module _foo
or adjusting the Extension declaration in your setup.py
from 或从以下位置调整
setup.py
的扩展声明
Extension('_foo',
to 至
Extension('foo',
I had the same error, however it was due to which python was being in use. 我有同样的错误,但是这是由于正在使用python。 I was using a system with python2.7, python3.4 and python3.5.
我正在使用带有python2.7,python3.4和python3.5的系统。 Only "python3" (symlinked to python3.5) worked.
只有“ python3”(符号为python3.5)起作用。 Importing with any of the other pythons gave the " ImportError: dynamic module does not define init function" error.
使用任何其他python进行导入都会出现“ ImportError:动态模块未定义初始化函数”错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.