1

I am trying to embed python 2.7.3 in C++ and use Numpy library and I obtain runtime error while importing Numpy for the second time. Here is a simple code example (as smallest as possible) :

#include <Python.h>

int main() {
    for(int i=0 ; i<2 ; i++) {
        Py_Initialize() ;
        PyImport_ImportModule("numpy");
        Py_Finalize() ;
    }
    return 0 ;
}

What's wrong with this ?

0

1 Answer 1

4

From the Py_Finalize documentation docs you have:

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.

I wouldn't be surprised if Numpy is one of these extensions.

Update: looks like it is, see this question.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.