gpt4 book ai didi

python - Py_initialize/Py_Finalize 不能与 numpy 一起工作两次

转载 作者:行者123 更新时间:2023-12-04 13:51:54 26 4
gpt4 key购买 nike

在以下代码的第二次调用中,我的应用程序段错误,所以我想我错过了一些东西:

Py_Initialize();
pName = PyString_FromString("comp_macbeth");
pModule = PyImport_Import(pName);
Py_DECREF(pName);

if(pModule == NULL) {
PyErr_Print();
Py_Finalize();
return;
}

pFunc = PyObject_GetAttrString(pModule, "compute");
/* pFunc is a new reference */

if (!pFunc || !PyCallable_Check(pFunc) ) {
PyErr_Print();
Py_Finalize();
return;
}

Py_Finalize();

comp_macbeth.py 正在导入 numpy。如果我删除 numpy 导入,一切都很好。这是一个 numpy 错误,还是我错过了一些关于进口的东西?

最佳答案

来自 Py_Finalize docs :

Some extensions may not work properly if their initialization routine is called more than once; this can happen if an application calls Py_Initialize() and Py_Finalize() more than once.



显然 Numpy 就是其中之一。另见 this message来自 Numpy 讨论。

调用 Py_Initialize()仅一次,并清理 at exit ,是要走的路。 (而且它也应该更快!)

关于python - Py_initialize/Py_Finalize 不能与 numpy 一起工作两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68562396/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com