python - SWIG 和 Python3 导入错误

标签 python python-3.x swig

我用 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/

相关文章:

python - 处理非常大(超过 30GB)的文本文件并显示进度的最佳方式是什么

python - 为什么当我打印字典的键时,它前面有 dict_keys ?

python - 无法弄清楚字符串替换在 Python 3.x 中的工作原理

python - SWIG 文件更改后如何重建项目?

python - swig 生成 python 文件加 C#

python - 将 ifconfig 解析为列表

python - 使用 cuda 10 安装 pip tensorflow-gpu 后出错

python - 将多元函数根的求根算法中的两个根之一括起来

Python map 对象不可下标

javascript - 将字符串参数从 node.js 传递到 C++ 代码(使用 SWIG)