Cython compiled C extension: ImportError: dynamic module does not define init function
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
Solution 1:
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
.
Solution 2:
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.
Solution 3:
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.
Solution 4:
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.