Cython 编译 C 扩展:ImportError:动态模块未定义 init 函数
我刚刚使用 Cython 编译了部分 C 库作为扩展,作为“概念证明”。我设法破解了代码(除了常量正确性问题等),最终构建了一个扩展。
但是,当我尝试导入新创建的扩展时,出现以下错误:
ImportError: dynamic module does not define init function
我做错了什么以及如何解决此问题?
我在 Ubuntu 10.0.4 上使用 Cythn 0.11.2 和 Python 2.6.5
I have just compiled part of my C library as an extension using Cython, as a "proof of concept". I managed to hack the code (const correctnes problems etc aside), to finally get an extension built.
However, when I attempted to import the newly created extension, I got the following error:
ImportError: dynamic module does not define init function
What am I doing wrong and how do I fix this?
I am using Cythn 0.11.2 and Python 2.6.5 on Ubuntu 10.0.4
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我发现此问题的一个常见原因是,当使用 distutils 安装文件编译代码时,.pyx 基本名称与扩展名不匹配,例如:
为了避免该问题,扩展名应该与同样,在本例中为 cython_ext。
I've found that a frequent cause of this problem is, when using a distutils setup file to compile the code, that the .pyx base name does not match the extension name, e.g:
To avoid the problem the extension name should be exactly the same, in this case,
cython_ext
.看来这是 Cython 中的错误/功能。我有同样的事情,但只是添加:
到我的 .pyx 文件的顶部,问题就消失了。如果没有全局初始化(cinit 或设置全局变量),则不会生成所需的初始化代码。
It appears that it's a bug/feature in Cython. I had the same thing, but simply added:
to the top of my .pyx file and the issue went away. It appears if there is no global initialization (a cinit or setting a global variable), that the required initialization code isn't generated.
这是一个很晚的答案 - 但我遇到了同样的错误,当我使用 __cinit__ 而不是 __init__ 时,我的错误消失了。我仍在了解扩展类型,所以目前我不知道为什么会发生这种情况。 :) (您可以查看 http:// docs.cython.org/src/reference/extension_types.html#initialization-cinit-and-init)希望这对某人有用。
This is a very late answer - but I just had the same error, and mine went away when I used
__cinit__
instead of__init__
. I'm still learing about extension types so currently I do not know why this happens. :) (You can take a look at http://docs.cython.org/src/reference/extension_types.html#initialization-cinit-and-init) Hope this is useful to someone.在我的例子中,另一个非常晚的答案是我不小心在运行 python2 的终端中调用了 cython,同时尝试使用 python3 从另一个 python 环境上的终端使用生成的库。
到处使用相同的 python 版本修复了它。
Another really late answer in my case I had accidentally called cython in a terminal that was running python2, while trying to use the generated library from a terminal that was on another python environment, using python3.
Using the same python version everywhere fixed it.
同样是一个迟到的答案......但我一直在寻找特别回到这个问题的方法。这可能与 Dologan 解决的名称不匹配问题有关。
在我的例子中发生的事情是,我正在改编一个我已经开始工作的示例,并得到了
module does not Define init function
错误。这是通过使用(例如)验证的在此命令的输出中,我搜索了“init”并发现
我已从
setup.py
和 .pyx 文件中删除了“example”的所有实例,但即使从site-packages< 中删除扩展名后,这种情况仍然存在/code>,删除 build 和 dist 文件夹等。我终于发现从我的 .pyx 文件生成的 .cpp 文件仍然引用了中的类名的例子。一旦我重新运行我的
setup.py
,导入就可以工作,并且 .so 文件确实包含Likewise a late answer... but I kept finding my way back to this question in particular. It probably is related to the mismatched names issue that Dologan addresses.
What happened in my case was that I was adapting an example I'd gotten to work, and got the
module does not define init function
error. This was verified by using (e.g.)In this command's output I searched for 'init' and found
I had removed all instances of 'example' from my
setup.py
and .pyx file, but this persisted even after removing the extension fromsite-packages
, removing the build and dist folders, etc. I finally found that the .cpp file being generated from my .pyx file was still referring to the class name in the example. Once I reran mysetup.py
, import works, and indeed the .so file includes我也遇到过这个问题。确保您的 Cython 文件至少包含以下内容之一:
def
cdef class
)a=None
或记录器加载否则,Cython 将不会生成加载模块所需的
PyInit
例程,因此 Python 无法导入该模块。I've also ran into this problem. Make sure that your Cython file contains at least one of the following:
def
cdef class
)a=None
or a logger loadOtherwise Cython won't generate a
PyInit
routine needed to load the module, and as such the module won't be importable by Python.我遇到了同样的错误,并通过在“在专用控制台中执行”模式下运行主 .py 脚本来解决。在“工具”-“首选项”-“运行”中可用。
I had the same error and was solved by running the main .py script in "Execute in a dedicated console " mode. Available in Tools - Preferences - Run.
通过向函数添加文档字符串可以解决此问题。
This is solved by adding a doc-string to your functions.
我通过
希望它有帮助来解决它。
I solve it by
Hope it helps.