编译器找不到Py_InitModule()..它是否已被弃用,如果是这样,我应该使用什么?

我正在尝试为python编写C扩展。 使用代码(如下),我收​​到编译器警告:

implicit declaration of function 'Py_InitModule' 

它在运行时失败并出现此错误:

 undefined symbol: Py_InitModule 

我花了几个小时寻找一个没有快乐的解决方案。 我尝试过多次语法修改,我甚至发现一篇post暗示该方法已被弃用。 但是我发现没有替代品。

这是代码:

 #include  //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"); } 

如果它有帮助,这是我的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]) 

test.py中的测试代码:

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

任何帮助都会非常感激。

您拥有的代码在Python 2.x中可以正常工作,但Python 3.x中不再使用Py_InitModule 。 现在,您创建一个PyModuleDef结构,然后PyModule_Create它的引用传递给PyModule_Create

结构看起来像:

 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 }; 

然后你的PyMODINIT_FUNC函数看起来像:

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

请注意, PyMODINIT_FUNC函数的名称必须是PyInit_的forms,其中是模块的名称。

我认为如果你阅读Python 3.x文档中的Extending是值得的。 它详细描述了如何在现代Python中构建扩展模块。

我遇到了与Py_InitModule()相同的问题。 我从前面提到的Python 3文档开始,特别是“扩展和嵌入Python解释器”文档。 但该文档的标题为“简单示例”的章节中遗漏了细节。 所以。 我用谷歌搜索了这篇简短的讲座:

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

这在很多方面更适合Python-C API扩展的新用户……除了它没有针对Python v3进行更新。 所以…请参阅scipy讲座,Python 3文档和StackOverflow讨论,根据您的需求剔除每个相关信息。