Issue
I have created a C extension to create a loop and print 10 objects. It is compiling successfully but when I import it in python program and compile it then the terminal gives me an error saying:
ImportError: dynamic module does not define module export function (PyInit_loop)
Using ubuntu 19.04 with Python 3.7. I have created a virtualenv and I am doing whole work in it. Couldn't find specific solution anywhere but I someone said that the C file is hooking up with the Python2.7 in my system but since I am using virtualenv I don't expect this to be the case. Doing it for the first time.
This is my C Extension File
#include <Python.h>
#include <stdio.h>
static PyObject* loop(PyObject* self)
{
int i =0;
for(i=0;i<10;i++)
{
printf("The number is %d\n",i);
}
Py_RETURN_NONE;
}
static char loop_docs[] = "loop(): This function is going to create a printing loop for 10 times.\n";
static PyMethodDef loop_methods[] = {
{"loop", (PyCFunction)loop, METH_NOARGS, loop_docs},
{NULL}
};
static struct PyModuleDef loop_module_def =
{
PyModuleDef_HEAD_INIT,
"loop",
"Module that is still in development",
-1,
loop_methods
};
PyMODINIT_FUNC PyInit_fibonacci(void){
Py_Initialize();
return PyModule_Create(&loop_module_def);
}
this is my setup.py
from distutils.core import setup, Extension
setup(name='loop', version='1.0',ext_modules=[Extension('loop', ['loop.c'])])
this is the file I am trying to run
import loop #<- Here Error Occurs
print(loop.loop())
Solution
The entry point of your native module (function with PyMODINIT_FUNC
) should have a name based on the module name.
Here, this function is named PyInit_fibonacci()
but your module is named loop
.
I would suggest to name your function PyInit_loop()
(or alternatively name your module fibonacci
).
(https://docs.python.org/3/extending/building.html#c.PyInit_modulename)
Note that the error message shows the expected name for the missing function:
ImportError: dynamic module does not define module export function (PyInit_loop)
Answered By - prog-fh
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.