编译器找不到 Py_InitModule() .. 它是否已弃用?如果是,我应该使用什么?
Compiler can't find Py_InitModule() .. is it deprecated and if so what should I use?
我正在尝试为 python 编写 C 扩展。使用代码(下面)我得到编译器警告:
implicit declaration of function ‘Py_InitModule’
它在 运行 时失败并出现此错误:
undefined symbol: Py_InitModule
我花了好几个小时寻找解决方案,但没有任何乐趣。我已尝试对语法进行多项细微更改,我什至发现 post 表明该方法已被弃用。但是我找不到替代品。
代码如下:
#include <Python.h>
//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 中可以正常工作,但 Py_InitModule
不再用于 Python 3.x。现在,你创建了一个 PyModuleDef
structure and then pass a reference to it to 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_<name>
的形式,其中 <name>
是您的模块的名称。
我认为如果您阅读 Python 3.x 文档中的 Extending 是值得的。它详细描述了如何在现代 Python.
中构建扩展模块。
我 运行 遇到了与 Py_InitModule() 相同的问题。我从前面提到的 Python 3 个文档开始,特别是 "Extending and Embedding the Python Interpreter" 文档。但是该文档标题为 "A Simple Example" 的章节没有详细说明。所以。我用谷歌搜索了这个 scipy 讲座:
http://www.scipy-lectures.org/advanced/interfacing_with_c/interfacing_with_c.html
这在很多方面更适合 Python-C API 扩展的新手...除了它还没有更新到 Python v3。所以...请参阅 scipy 讲座和 Python 3 个文档,以及这个 Whosebug 讨论,根据您的需要从每一个中挑选相关信息。
我正在尝试为 python 编写 C 扩展。使用代码(下面)我得到编译器警告:
implicit declaration of function ‘Py_InitModule’
它在 运行 时失败并出现此错误:
undefined symbol: Py_InitModule
我花了好几个小时寻找解决方案,但没有任何乐趣。我已尝试对语法进行多项细微更改,我什至发现 post 表明该方法已被弃用。但是我找不到替代品。
代码如下:
#include <Python.h>
//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 中可以正常工作,但 Py_InitModule
不再用于 Python 3.x。现在,你创建了一个 PyModuleDef
structure and then pass a reference to it to 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_<name>
的形式,其中 <name>
是您的模块的名称。
我认为如果您阅读 Python 3.x 文档中的 Extending 是值得的。它详细描述了如何在现代 Python.
中构建扩展模块。我 运行 遇到了与 Py_InitModule() 相同的问题。我从前面提到的 Python 3 个文档开始,特别是 "Extending and Embedding the Python Interpreter" 文档。但是该文档标题为 "A Simple Example" 的章节没有详细说明。所以。我用谷歌搜索了这个 scipy 讲座:
http://www.scipy-lectures.org/advanced/interfacing_with_c/interfacing_with_c.html
这在很多方面更适合 Python-C API 扩展的新手...除了它还没有更新到 Python v3。所以...请参阅 scipy 讲座和 Python 3 个文档,以及这个 Whosebug 讨论,根据您的需要从每一个中挑选相关信息。