我用 C 编写了一个库。我使用 SWIG为 Java、Python 等生成绑定(bind)。我设法为这两种语言等编写了自定义类型映射。我还设法使用我的库(自定义协议(protocol))并与我用 C 编写的服务器、我用 Java 编写的客户端以及我用 Python 编写的客户端进行通信。
最近,我遇到了 solution 的多重继承问题。我发现很聪明。但是,当尝试使用 Python 3 复制错误时,错误消失了(也许 在版本 3 中已解决)。事实上,相同的代码、相同的项目和相同的源不会运行调用 python3
二进制文件,但它可以调用 python2.7 二进制文件。
我收到消息:
ImportError: dynamic module does not define init function (PyInit__pytellapic)
我已经阅读了 SWIG 文档中的含义,但错误略有不同:
import example
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ImportError: dynamic module does not define init function (init_example)
说:
This error is almost always caused when a bad name is given to the shared object file. For example, if you created a file example.so instead of _example.so you would get this error. Alternatively, this error could arise if the name of the module is inconsistent with the module name supplied with the %module directive. Double-check the interface to make sure the module name and the shared object filename match. Another possible cause of this error is forgetting to link the SWIG-generated wrapper code with the rest of your application when creating the extension module.
老实说,我认为这个可能的原因不适用于我的模块,因为它应该是 2.7 和 3 版本的 python 的错误,而不仅仅是 Python 3。
我将不胜感激任何建议,但考虑到 SWIG 似乎是一个过时的项目,我可能会继续使用 Python 2.7 和上述“hack”。
最好的问候,
最佳答案
生成的 C 模块是否定义了 PyInit__pytellapic
函数?
模块初始化方法在 Python 3 中发生变化时的名称和配置文件。如果您希望在 Python 2 和 Python 3 下运行完全相同的 C 代码,则必须同时包含旧名称和新名称。查看Migrating C Extensions章节了解更多信息。
根据 SWIG's documentation如果传递 -py3 参数,SWIG 2.0 应该会执行此操作,但我还没有尝试过。
关于python - SWIG 和 Python3 导入错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6845869/