Cython compiled C extension: ImportError: dynamic module does not define init function
NickName:Homunculus Reticulli Ask DateTime:2011-11-06T10:21:02

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

Copyright Notice:Content Author:「Homunculus Reticulli」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/8024805/cython-compiled-c-extension-importerror-dynamic-module-does-not-define-init-fu

Answers
Gertlex 2014-06-10T21:27:14

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.\n\nWhat 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.)\n\n\n nm -m build/lib.macosx-10.9-x86_64-2.7/myproj.so\n\n\nIn this command's output I searched for 'init' and found\n\n\n 000000000000c0d0 (__TEXT,__text) external _initexample\n\n\nI 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\n\n\n 000000000000c0a0 (__TEXT,__text) external _initmyproj\n",


Piotr Maślanka 2020-12-20T18:19:05

I've also ran into this problem. Make sure that your Cython file contains at least one of the following:\n\na normal Python def\na normal Python class (not cdef class)\na line of Python initialization, eg. a=None or a logger load\n\nOtherwise Cython won't generate a PyInit routine needed to load the module, and as such the module won't be importable by Python.",


aishwarya selvaraj 2018-11-15T09:39:15

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.",


GetLastError 2018-01-21T09:56:33

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


Dologan 2014-01-29T12:08:46

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:\n\next = Extension(name='different', sources=['cython_ext.pyx']) # Won't work\n\n\nTo avoid the problem the extension name should be exactly the same, in this case, cython_ext.",


Tim 2012-12-20T16:30:30

It appears that it's a bug/feature in Cython. I had the same thing, but simply added:\n\nSTUFF = \"Hi\"\n\n\nto 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.",


joon 2012-12-03T05:25:17

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.",


Cola_Colin 2018-04-15T19:18:29

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.\n\nUsing the same python version everywhere fixed it.",


More about “Cython compiled C extension: ImportError: dynamic module does not define init function” related questions

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.

Show Detail

What is the init function of a dynamic module in python?

I am getting the same error of these other two questions: ImportError: dynamic module does not define init function, but it does and Cython compiled C extension: ImportError: dynamic module does ...

Show Detail

Cython with Scikit-build: dynamic module does not define module export function

I have been doing some tests with the cython hello world example in scikit-build-sample-projects, and noticed that if I change the name of some files and their corresponding fields in CMakeLists.tx...

Show Detail

ImportError: dynamic module does not define init function (initfizzbuzz)

I tried to compile fizzbuzz.c, in order to import it by python. For building fizzbuzz.c,I used python setup.py build_ext -i. After building it, I tried to import fizzbuzz.c but the error below occ...

Show Detail

ImportError: dynamic module does not define init function (initfizzbuzz)

I tried to compile fizzbuzz.c, in order to import it by python. For building fizzbuzz.c,I used python setup.py build_ext -i. After building it, I tried to import fizzbuzz.c but the error below occ...

Show Detail

ImportError: dynamic module does not define init function Naming is correct

While trying to build python bindings with C, I have the following #if PY_VERSION_HEX >= 0x03000000 #define MOD_ERROR NULL #define MOD_INIT(name) PyObject* PyInit_##name(void) #define MOD_RETUR...

Show Detail

ImportError: dynamic module does not define init function

I am trying to reproduce the following tutorial https://csl.name/post/c-functions-python/. My Python extension in C++ looks like: #include <Python.h> static PyObject* py_myFunction(PyObject*

Show Detail

GAE ImportError: dynamic module does not define init function (init_mysql)

My staging GAE app throws the below err, File "/base/data/home/apps/foo156801/worker:20170301t222555.399535951340506041/lib/MySQLdb/__init__.py", line 19, in <module> import _mysql ImportErr...

Show Detail

Cython on Termux. ImportError: dynamic module does not define module export function (PyInit_libmc)

I'm trying to use python/cython on Android within Termux. I got python, cython pip-installed - with no 'obvious' problems. Now I have two simple test-snippets A 'main' file mct.py import os hm=os...

Show Detail

Cython ImportError in Jupyter

I'm trying to use Cython in a Jupyter Notebook but keep encountering an error along the lines of ImportError: dynamic module does not define module export function (

Show Detail