简体   繁体   English

无法导入使用SWIG创建的模块

[英]Can't import module created using SWIG

I'm trying to build python extension. 我正在尝试构建python扩展。 I've create simple library that export single function. 我创建了导出单个函数的简单库。 It's just a single file - testlib.c that implements function called 'apicall'. 它只是一个文件 - testlib.c,它实现了名为'apicall'的函数。 Then I create SWIG interface file: 然后我创建SWIG接口文件:

%module testlibpy                      
void apicall(const char* message);

Then I use this command to generate interface: swig -python -py3 -modern testlibpy.i 然后我用这个命令生成接口: swig -python -py3 -modern testlibpy.i

My setup.py looks like this: 我的setup.py看起来像这样:

from distutils.core import setup, Extension


example_module = Extension('_testlibpy',
                           sources=['testlibpy_wrap.c', 'testlib.c'],)

setup (name = 'testlibpy',
       version = '0.1',
       author      = "SWIG Docs",
       description = """Simple swig example from docs""",
       ext_modules = [example_module],
       py_modules = ["testlibpy"],
      ) 

I'm building extension using command: python3.4 ./setup.py build_ext --inplace . 我正在使用命令构建扩展: python3.4 ./setup.py build_ext --inplace Everything works fine. 一切正常。 When I'm trying to import my new extension from python3.4 command-line, I've get following error: 当我试图从python3.4命令行导入我的新扩展时,我得到以下错误:

Python 3.4.2 (default, Feb 18 2015, 04:50:08)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import testlibpy
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".../sandbox/swigtest/testlibpy.py", line 24, in <module>
    _testlibpy = swig_import_helper()
  File ".../sandbox/swigtest/testlibpy.py", line 20, in swig_import_helper
    _mod = imp.load_module('_testlibpy', fp, pathname, description)
  File ".../swt/install/python-3.4.2/lib/python3.4/imp.py", line 243, in load_module
    return load_dynamic(name, filename, file)
ImportError: .../sandbox/swigtest/_testlibpy.cpython-34m.so: undefined symbol: PyCObject_FromVoidPtr
>>>

Everything works fine for other python versions - 2.7 and 3.0. 一切适用于其他python版本 - 2.7和3.0。 SWIG Version 1.3.40 SWIG版本1.3.40

In Python 3.2 the CObject API was removed. 在Python 3.2中,删除了CObject API It was replaced with the Capsule API , which is also available for Python 2.7 and 3.1. 它被Capsule API取代,后者也可用于Python 2.7和3.1。

The older version of SWIG that you are using will be generating code using the CObject API that Python 3.4 doesn't have, hence causing the error when you import it and python can't find the function PyCObject_FromVoidPtr . 您正在使用的旧版SWIG将使用Python 3.4没有的CObject API生成代码,因此在导入时会导致错误并且python无法找到函数PyCObject_FromVoidPtr

The solution would be to use a version of SWIG >= 2.0.4 to generate code for Python 3.2 and above. 解决方案是使用SWIG> = 2.0.4的版本为Python 3.2及更高版本生成代码。

From the SWIG Changelog 来自SWIG Changelog

Version 2.0.4 (21 May 2011) 版本2。0。4(2011年5月21日)

2011-04-09: szager 2011-04-09:szager
[Python] Applied patch #1932484: migrate PyCObject to PyCapsule. [Python]应用补丁#1932484:将PyCObject迁移到PyCapsule。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM