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 ?

1 Answers1

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.

Community
  • 1
  • 1
Tarantula
  • 19,031
  • 12
  • 54
  • 71