.so模块在python中没有导入:动态模块没有定义init函数

hig*_*dth 7 c c++ python python-c-api python-extensions

我正在尝试为C函数编写一个python包装器.在编写完所有代码并将其编译后,Python无法导入该模块.我按照这里给出的例子.在修复一些拼写错误后,我在这里重现它.有一个文件myModule.c:

#include <Python.h>

/*
 * Function to be called from Python
 */
static PyObject* py_myFunction(PyObject* self, PyObject* args)
{
    char *s = "Hello from C!";
    return Py_BuildValue("s", s);
}
/*
 * Bind Python function names to our C functions
 */
static PyMethodDef myModule_methods[] = {
    {"myFunction", py_myFunction, METH_VARARGS},
    {NULL, NULL}
};

/*
 * Python calls this to let us initialize our module
 */
void initmyModule()
{
    (void) Py_InitModule("myModule", myModule_methods);
}
Run Code Online (Sandbox Code Playgroud)

由于我在使用Macports python的Mac上,我将其编译为

$ g++ -dynamiclib -I/opt/local/Library/Frameworks/Python.framework/Headers -lpython2.6 -o myModule.dylib myModule.c
$ mv myModule.dylib myModule.so
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试导入它时出现错误.

$ ipython
In[1]: import myModule
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)

/Users/.../blahblah/.../<ipython console> in <module>()

ImportError: dynamic module does not define init function (initmyModule)
Run Code Online (Sandbox Code Playgroud)

为什么我不能导入它?

Fré*_*idi 5

由于您使用的是C++编译器,函数名会被错位(例如,我的g++轧液void initmyModule()进入_Z12initmyModulev).因此,python解释器将找不到模块的init函数.

您需要使用普通的C编译器,或者使用extern"C"指令强制整个模块中的C链接:

#ifdef __cplusplus
extern "C" {
#endif 

#include <Python.h>

/*
 * Function to be called from Python
 */
static PyObject* py_myFunction(PyObject* self, PyObject* args)
{
    char *s = "Hello from C!";
    return Py_BuildValue("s", s);
}

/*
 * Bind Python function names to our C functions
 */
static PyMethodDef myModule_methods[] = {
    {"myFunction", py_myFunction, METH_VARARGS},
    {NULL, NULL}
};

/*
 * Python calls this to let us initialize our module
 */
void initmyModule()
{
    (void) Py_InitModule("myModule", myModule_methods);
}

#ifdef __cplusplus
}  // extern "C"
#endif 
Run Code Online (Sandbox Code Playgroud)

  • 文档中给出的`PyMODINIT_FUNC`宏将为您处理. (2认同)