Compiler can't find Py_InitModule() .. is it deprecated and if so what should I use?

隐身守侯 提交于 2019-11-30 11:12:57

The code you have would work fine in Python 2.x, but Py_InitModule is no longer used in Python 3.x. Nowadays, you create a PyModuleDef structure and then pass a reference to it to PyModule_Create.

The structure would look like:

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

And then your PyMODINIT_FUNC function would look like:

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

Note that the name of the PyMODINIT_FUNC function must be of the form PyInit_<name> where <name> is the name of your module.

I think it would be worthwhile if you read Extending in the Python 3.x documentation. It has a detailed description of how to build extension modules in modern Python.

I ran into the same problem with Py_InitModule(). I started with the aforementioned Python 3 docs, specifically the "Extending and Embedding the Python Interpreter" doc. But that doc's chapter entitled "A Simple Example" leaves out details. So. I googled this scipy lecture:

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

which is in many ways more suitable for someone new to Python-C API extensions ... except it has not been updated for Python v3. So ... consult the scipy lecture, and the Python 3 docs, and this StackOverflow discussion, culling the pertinent information from each for your needs.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!