Cython 编译 C 扩展:ImportError:动态模块未定义 init 函数

发布于 2024-12-14 02:07:00 字数 274 浏览 4 评论 0原文

我刚刚使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

给我一枪 2024-12-21 02:07:00

我发现此问题的一个常见原因是,当使用 distutils 安装文件编译代码时,.pyx 基本名称与扩展名不匹配,例如:

ext = Extension(name='different', sources=['cython_ext.pyx']) # Won't work

为了避免该问题,扩展名应该与同样,在本例中为 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:

ext = Extension(name='different', sources=['cython_ext.pyx']) # Won't work

To avoid the problem the extension name should be exactly the same, in this case, cython_ext.

染柒℉ 2024-12-21 02:07:00

看来这是 Cython 中的错误/功能。我有同样的事情,但只是添加:

STUFF = "Hi"

到我的 .pyx 文件的顶部,问题就消失了。如果没有全局初始化(cinit 或设置全局变量),则不会生成所需的初始化代码。

It appears that it's a bug/feature in Cython. I had the same thing, but simply added:

STUFF = "Hi"

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.

一抹淡然 2024-12-21 02:07:00

这是一个很晚的答案 - 但我遇到了同样的错误,当我使用 __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.

如果没有 2024-12-21 02:07:00

在我的例子中,另一个非常晚的答案是我不小心在运行 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.

善良天后 2024-12-21 02:07:00

同样是一个迟到的答案......但我一直在寻找特别回到这个问题的方法。这可能与 Dologan 解决的名称不匹配问题有关。

在我的例子中发生的事情是,我正在改编一个我已经开始工作的示例,并得到了 module does not Define init function 错误。这是通过使用(例如)验证的

nm -m build/lib.macosx-10.9-x86_64-2.7/myproj.so

在此命令的输出中,我搜索了“init”并发现

000000000000c0d0 (__TEXT,__text) 外部_initexample

我已从 setup.py 和 .pyx 文件中删除了“example”的所有实例,但即使从 site-packages< 中删除扩展名后,这种情况仍然存在/code>,删除 build 和 dist 文件夹等。我终于发现从我的 .pyx 文件生成的 .cpp 文件仍然引用了中的类名的例子。一旦我重新运行我的 setup.py,导入就可以工作,并且 .so 文件确实包含

000000000000c0a0 (__TEXT,__text) 外部_initmyproj

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.)

nm -m build/lib.macosx-10.9-x86_64-2.7/myproj.so

In this command's output I searched for 'init' and found

000000000000c0d0 (__TEXT,__text) external _initexample

I had removed all instances of 'example' from my setup.py and .pyx file, but this persisted even after removing the extension from site-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 my setup.py, import works, and indeed the .so file includes

000000000000c0a0 (__TEXT,__text) external _initmyproj

野鹿林 2024-12-21 02:07:00

我也遇到过这个问题。确保您的 Cython 文件至少包含以下内容之一:

  • 普通的 Python def
  • 普通的 Python 类(不是 cdef class
  • Python 初始化行,例如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:

  • a normal Python def
  • a normal Python class (not cdef class)
  • a line of Python initialization, eg. a=None or a logger load

Otherwise Cython won't generate a PyInit routine needed to load the module, and as such the module won't be importable by Python.

人间☆小暴躁 2024-12-21 02:07:00

我遇到了同样的错误,并通过在“在专用控制台中执行”模式下运行主 .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.

勿忘心安 2024-12-21 02:07:00

通过向函数添加文档字符串可以解决此问题。

This is solved by adding a doc-string to your functions.

总以为 2024-12-21 02:07:00

我通过

def cinit(self): pass

希望它有帮助来解决它。

I solve it by

def cinit(self): pass

Hope it helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文