ImportError: dynamic module does not define init function (initfizzbuzz)
NickName:SamuraiT Ask DateTime:2014-06-15T10:17:52

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 occurred. How can I solve this problem ?

Error

>>> import fizzbuzz
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: dynamic module does not define init function (initfizzbuzz)

fizzbuzz.c

#include <stdio.h>

void fizzbuzz(int n){

    for (int i=1; i <= n; i++){
        if (i % 3 == 0 && i % 5 ==0){
            printf("fizzbuzz %d \n", i);
        }
        else if (i % 3 == 0){
            printf("fizz %d \n", i);
        }
        else if(i % 5 == 0){
            printf("buzz %d \n", i);
        }
    }
}

setup.py

from distutils.core import setup, Extension
module = Extension('fizzbuzz', ['fizzbuzz.c'])
setup(
      name='fizzbuzz',
      version='1.0',
      ext_modules=[module],
)

Copyright Notice:Content Author:「SamuraiT」,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/24226001/importerror-dynamic-module-does-not-define-init-function-initfizzbuzz

Answers
philosopher 2015-08-20T09:37:25

The error also occurs, when using boost::python, if the module name is different to the compiled .so file name. For example:\n\nhello.cpp\n\n#include <boost/python/module.hpp>\n#include <boost/python/def.hpp>\nusing namespace std;\nusing namespace boost::python;\n\nint helloWorld(){\n cout << \"Hello world!\" << endl;\n return 0;\n}\n\nBOOST_PYTHON_MODULE(libhello) {\n def(\"hello_world\", helloWorld);\n}\n\n\ncompilation command:\n\ng++ -fpic -shared -o libfoo.so -Wl,-soname,\"libfoo.so\" hello.cpp -I<path/to/python> -L/usr/local/lib -lboost_python-py34\n\n\nWhen including in python with import libfoo the following error occurs:\n\nImportError: dynamic module does not define init function (PyInit_libfoo)\n\n\nThis is because of \"libhello\" and \"libfoo\" do not match. ",


Ivan Klass 2016-05-05T11:26:43

Worth notify - same error can occur if library is compiled for different python version. For example, if shared object is for python 3, but you try to import module from python 2.",


Martijn Pieters 2014-06-15T02:27:33

Python doesn't and cannot support arbitrary C files as modules. You'll have to follow certain conventions to let Python know what functions your module supports.\nTo do so, Python will look for a init<name> function, where <name> is the module name. Python was looking for initfizzbuzz but failed to find it, so loading the module failed.\nApart from an initialiser, you also need to provide a structure detailing what functions are available, and your function will need to handle Python types as arguments. Python provides you with the necessary utility functions and defines to make that easy enough.\nI strongly urge you follow the Extending and Embedding the Python Interpreter tutorial. It teaches you everything you need to know to make your fizzbuzz C code work as a Python module.",


Aditya Mittal 2017-01-07T08:24:54

do python3 ./yourpythonscript\n\ninstead of\n\npython ./yourpythonscript\n\neven if you have python aliased as python3\n\nThe name must be exact with which you compile boost and boost-python:\nbrew reinstall boost --with-python3 --without-python\nbrew reinstall boost-python --with-python3 --without-python",


lackadaisical 2014-10-20T11:00:47

You should define a function named init_fizzbuzz, that should contain the code to initialize the module. This function should also call Py_InitModule, to setup the bindings for the c functions in Python. For further info, check out this tutorial.\n\nIn yor case, your code should be adapted to something like this:\n\nstatic PyObject* py_fizzbuzz(PyObject* self, PyObject* args)\n{\n int value;\n if (!PyArg_ParseTuple(args, \"i\", &value))\n return NULL;\n for (int i=1; i <= n; i++){\n if (i % 3 == 0 && i % 5 ==0){\n printf(\"fizzbuzz %d \\n\", i);\n }\n else if (i % 3 == 0){\n printf(\"fizz %d \\n\", i);\n }\n else if(i % 5 == 0){\n printf(\"buzz %d \\n\", i);\n }\n }\n\n // Return value.\n return Py_BuildValue(\"i\", 0);\n\n}\n\n// Mapping between python and c function names. \nstatic PyMethodDef fizzbuzzModule_methods[] = {\n {\"fizzbuzz\", py_fizzbuzz, METH_VARARGS},\n {NULL, NULL}\n };\n\n// Module initialisation routine.\nvoid init_fizzbuzz(void)\n{\n // Init module.\n (void) Py_InitModule(\"fizzbuzz\", fizzbuzzModule_methods);\n\n}\n",


More about “ImportError: dynamic module does not define init function (initfizzbuzz)” related questions

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

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 &lt;module&gt; import _mysql ImportErr...

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 &gt;= 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 (initlibpyuno)

I have installed Eclipse IDE with Python, I'm trying to setup an environment for writing some python scripts to automate Libre Office, I have made a script with just one line of code for now ("import

Show Detail

Pylibmc: ImportError: dynamic module does not define init function (init_pylibmc)

&gt;&gt;&gt; import pylibmc Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packa

Show Detail

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

I am getting this error when I deployed my python app with Flask on Google AppEngine. I will be grateful if someone help me. ps: My local server works like a charm File "/base/data/home/apps/s~dw...

Show Detail

ImportError dynamic module does not define init function (boost.python)

Thank you for see this topic. I tried to use boost.python but now this error happens. ImportError: dynamic module does not define init function (initlatticepy) I checked some questions in we...

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