简体   繁体   English

SWIG + setup.py:ImportError:动态模块未定义初始化函数(init_foo)

[英]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.

相关问题 ImportError:动态模块没有定义init函数 - ImportError: dynamic module does not define init function c 程序 SWIG 到 python 给出&#39;ImportError:动态模块没有定义初始化函数&#39; - c program SWIG to python gives 'ImportError: dynamic module does not define init function' (从Swig到python)导入错误:动态模块未定义init函数 - (Swig to python) import error:dynamic module does not define init function ImportError:动态模块没有定义init函数,但确实如此 - ImportError: dynamic module does not define init function, but it does 导入错误:动态模块未定义 init 函数 (initfizzbuzz) - ImportError: dynamic module does not define init function (initfizzbuzz) ImportError:动态模块未定义初始化函数(initlibpyuno) - ImportError: dynamic module does not define init function (initlibpyuno) Google AppEngine ImportError:动态模块没有定义init函数(init_mysql) - Google AppEngine ImportError: dynamic module does not define init function (init_mysql) GAE ImportError:动态模块未定义初始化函数(init_mysql) - GAE ImportError: dynamic module does not define init function (init_mysql) Pylibmc:ImportError:动态模块未定义初始化函数(init_pylibmc) - Pylibmc: ImportError: dynamic module does not define init function (init_pylibmc) python import swig库因动态模块未定义初始化函数而失败 - python import swig library fails with dynamic module does not define init function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM