Crash when importing numpy from the Python C-API after calling Py_Finilize()
See original GitHub issueHi,
We are getting a crash in the numpy code when importing numpy after Py_Finalize() is called. If we comment out the call to Py_Finalize() the second import will be successful.
#include "python2.7/Python.h"
int RunPythonScript(const char * pszScript)
{
Py_Initialize();
int nResult = PyRun_SimpleStringFlags(pszScript, NULL);
Py_Finalize();
return nResult;
}
int main(void)
{
RunPythonScript("import numpy");
printf("First import of numpy ran successfully!\n");
RunPythonScript("import numpy");
printf("Second import of numpy ran successfully!\n");
return 0;
}
I compiled this example using GCC: gcc -o numpy_crash numpy_crash.cpp -lpython2.7
This simple example should not crash. My guess is that Python will destroy some global objects when Py_Finalize() is called. But these global objects are still referenced in the numpy code. You would need to register an atexit function using Py_AtExit() to reinitialize any global objects to NULL so that they are re-created on subsequent import of numpy.
This bug is most likely the same bug as #656 and #3961. But we are only running a single Python interpreter. We call Py_Finalize() every time a user runs a new Python script so that the variables they defined on the previous runs are no longer available in the Python environment.
Regards,
Phelippe Neveu
Issue Analytics
- State:
- Created 7 years ago
- Reactions:2
- Comments:21 (4 by maintainers)
Top GitHub Comments
Same issue here. It is quite disappointing for me that this is not working, and so there really is no good solution to re-initialize the embedded python interpreter for scripts using numpy.
The use case here is an application which embeds a python interpreter for scripting vision algorithms. Unfortunately, this bug forces us to stop and start the application each time we make changes to the scripts. I know there is importlib.reload, but it is not really usable either for complex module dependencies.
Note: Without numpy, the Initialize - Finalize works great and we don’t need to restart after script editing.
I don’t know, is there maybe a good solution out there using sockets and seperate processes? It looks like this problem must be hitting a lot of people (i.e., everyone who wants to use embedded python together with numpy).
Hi, I experience exactly the same behavior as Phelippe. I embed execution of python scripts into a C application. If numpy is imported in the script, the application crashes the second time the script is executed. Each time a script is run, pyInitialize and pyFinalize are called. The only way to make it work is to stop and restart our application before running the script a second time which is not the way it should work. It would be nice to have a fix. Regards, Alain