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* self, PyObject* args)
{
char *s = "Hello from C!";
return Py_BuildValue("s", s);
}
static PyObject* py_myOtherFunction(PyObject* self, PyObject* args)
{
double x, y;
PyArg_ParseTuple(args, "dd", &x, &y);
return Py_BuildValue("d", x*y);
}
static PyMethodDef extPy_methods[] = {
{"myFunction", py_myFunction, METH_VARARGS},
{"myOtherFunction", py_myOtherFunction, METH_VARARGS},
{NULL, NULL}
};
void initextPy(void)
{
(void) Py_InitModule("extPy", extPy_methods);
}
I am using the following setup.py:
from distutils.core import setup, Extension
setup(name='extPy', version='1.0', \
ext_modules=[Extension('extPy', ['extPy.cpp'])])
After invoking it with python setup.py install
the .so file seems to be in the right place.
But when I try to use this extension with import extPy
I get the Error:
ImportError: dynamic module does not define init function
What am I missing here? Thanks for Help.
Copyright Notice:Content Author:「P Alcove」,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/28040833/importerror-dynamic-module-does-not-define-init-function