question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Crash when importing numpy from the Python C-API after calling Py_Finilize()

See original GitHub issue

Hi,

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:open
  • Created 7 years ago
  • Reactions:2
  • Comments:21 (4 by maintainers)

github_iconTop GitHub Comments

8reactions
wiedemannccommented, Jul 31, 2018

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).

6reactions
gyp33commented, Jan 12, 2018

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python C API crashes on 'import numpy' when initilizing ...
For example if I have a program that allows the user to run a custom python script, it should be possible to run...
Read more >
Trouble when reloading extension modules. - Issue Tracker
For my use case, I noticed that when using numpy with Py_Initialize() and Py_Finalize(): Py_Initialize() // call code importing numpy ...
Read more >
Troubleshooting ImportError — NumPy v1.24 Manual
Importing the numpy c-extensions failed. This error can happen for different reasons, often due to issues with your setup. The error also has...
Read more >
Matlab crashing when importing numpy - MathWorks
When running this simple matlab code: function out = python_test(). py.importlib.import_module('numpy'). I get the following following error:.
Read more >
12. Writing a C extension to NumPy
This call makes sure that the module which implements the array type has been imported, and initializes a pointer array through which the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found