The compiler cannot find Py_InitModule () .. is it deprecated, and if so, what should I use?

I am trying to write a C extension for python. With the code (below), I get a compiler warning:

implicit declaration of function 'Py_InitModule' 

And it does not work at runtime with this error:

 undefined symbol: Py_InitModule 

I spent literally hours finding a solution without joy. I tried a few minor syntax changes, I even found a message stating that the method is deprecated. However, I do not find a replacement.

Here is the code:

 #include <Python.h> //a func to calc fib numbers int cFib(int n) { if (n<2) return n; return cFib(n-1) + cFib(n-2); } static PyObject* fib(PyObject* self,PyObject* args) { int n; if (!PyArg_ParseTuple(args,"i",&n)) return NULL; return Py_BuildValue("i",cFib(n)); } static PyMethodDef module_methods[] = { {"fib",(PyCFunction) fib, METH_VARARGS,"calculates the fibonachi number"}, {NULL,NULL,0,NULL} }; PyMODINIT_FUNC initcModPyDem(void) { Py_InitModule("cModPyDem",module_methods,"a module"); } 

If this helps, this is my setup.py:

 from distutils.core import setup, Extension module = Extension('cModPyDem', sources=['cModPyDem.c']) setup(name = 'packagename', version='1.0', description = 'a test package', ext_modules = [module]) 

And the test code in test.py:

 import cModPyDem if __name__ == '__main__' : print(cModPyDem.fib(200)) 

Any help would be very, very much appreciated.

+7
c python python-extensions
source share
2 answers

The code you have will work fine in Python 2.x, but Py_InitModule no longer used in Python 3.x. You are currently creating a PyModuleDef structure and then passing a reference to it PyModule_Create .

The structure will look like this:

 static struct PyModuleDef cModPyDem = { PyModuleDef_HEAD_INIT, "cModPyDem", /* name of module */ "", /* module documentation, may be NULL */ -1, /* size of per-interpreter state of the module, or -1 if the module keeps state in global variables. */ module_methods }; 

And then your PyMODINIT_FUNC function will look like this:

 PyMODINIT_FUNC PyInit_cModPyDem(void) { return PyModule_Create(&cModPyDem); } 

Please note that the name of the PyMODINIT_FUNC function should be of the form PyInit_<name> , where <name> is the name of your module.

I think it would be helpful if you read Extending in the Python 3.x documentation. It contains a detailed description of how to create extension modules in modern Python.

+21
source share

I ran into the same problem with Py_InitModule (). I started working with the above Python 3 documents, in particular with the document "Extending and Implementing a Python Interpreter Document". But this chapter of the chapter entitled โ€œA Simple Exampleโ€ does not provide details. So. I studied this meager lecture:

http://www.scipy-lectures.org/advanced/interfacing_with_c/interfacing_with_c.html

which is much more suitable for someone new to the Python-C API extensions ... except that it has not been updated for Python v3. So ... consult the lean lecture and the Python 3 docs and this discussion on StackOverflow, discarding the relevant information from each for your needs.

0
source share

All Articles