SWIG 和 Python3 导入错误
我用 C 编写了一个库。我使用 SWIG 生成 Java、Python 等的绑定。我设法编写了自定义类型两种语言的地图等等。我还设法使用我的库(自定义协议)并与我用 C 编写的服务器、我用 Java 编写的客户端以及我用 Python 编写的客户端进行通信。
最近,我遇到了一个多重继承问题,使用了解决方案我发现很聪明。但是,当尝试使用 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)
说:
这个错误几乎总是由给了一个错误的名字引起的 共享对象文件。例如,如果您创建了文件 example.so 而不是 _example.so 你会得到这个错误。或者,这个 如果模块名称与模块名称不一致,可能会出现错误 %module 指令提供的模块名称。仔细检查 接口以确保模块名称和共享对象文件名 匹配。导致此错误的另一个可能原因是忘记链接 SWIG 生成的包装器代码与应用程序的其余部分 创建扩展模块。
老实说,我认为这个可能的原因不适用于我的模块,因为它应该是 python 2.7 和 3 版本的错误,而不仅仅是 Python 3。
我将不胜感激任何建议,但考虑到 SWIG 似乎是一个错误过时的项目,可能我会继续使用 Python 2.7 和提到的“hack”。
此致,
I have written a library in C. I use SWIG to generate bindings for Java, Python, etc. I managed to write custom type maps for both languages and so on. I also managed to use my library (a custom protocol) and communicate with a server that I wrote in C, with a client that I wrote in Java and, with a client that I'm writing in Python.
Recently, I came across a multiple inheritance problem with a solution I found smart. But, when trying to replicate the error using Python 3, the error was gone (maybe in version 3 is solved). The fact is that, the same code, the same project and the same source won't run invoking python3
binary, but it works invoking python2.7 binary.
I get the message:
ImportError: dynamic module does not define init function (PyInit__pytellapic)
Which I already read what could mean from SWIG documentation with a slightly different error:
import example
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ImportError: dynamic module does not define init function (init_example)
Saying that:
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.
Honestly, I think that this probable causes won't apply to my modules, as it should be an error for 2.7 and 3 versions of python, not just for Python 3.
I would appreciate any advice, but considering that SWIG seems to be an outdated project, probably I'll continuing using Python 2.7 with the mentioned "hack".
Best regards,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
生成的 C 模块是否定义了
PyInit__pytellapic
函数?名称和配置文件(如果模块 init 方法在 Python 3 中已更改)。如果您希望完全相同的 C 代码在 Python 2 和 Python 3 下运行,则必须同时包含旧名称和新名称。有关详细信息,请参阅迁移 C 扩展一章。
根据 SWIG 文档 如果您传递 -py3 参数,SWIG 2.0 应该执行此操作,但我还没有尝试过。
Does the C module generated have a
PyInit__pytellapic
function defined?The name and profile if the module init method has changed in Python 3. If you want the exact same C-code to run under both Python 2 and Python 3 you must include both the old and the new name. See the Migrating C Extensions chapter for more info.
According to SWIG's documentation SWIG 2.0 should do this if you pass the -py3 parameter, but I haven't tried it.