Tôi đang cố gắng tạo lại hướng dẫn sau https://csl.name/post/c-functions-python/.NhậpError: mô-đun động không xác định chức năng init
My Python phần mở rộng trong C++ trông giống như:
#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);
}
Tôi đang sử dụng setup.py sau:
from distutils.core import setup, Extension
setup(name='extPy', version='1.0', \
ext_modules=[Extension('extPy', ['extPy.cpp'])])
Sau khi gọi nó với python setup.py install
file .so dường như ở đúng nơi.
Nhưng khi tôi cố gắng sử dụng phần mở rộng này với import extPy
tôi nhận được Lỗi:
ImportError: mô-đun năng động không xác định hàm init
tôi đang thiếu gì ở đây? Cảm ơn vì sự giúp đỡ.
này đã làm công việc. Cảm ơn bạn. –