I've this c++ code. It has import numpy in a function which is called twice.
#include <iostream>
#include <pybind11/embed.h>
namespace py = pybind11;
void test() {
py::scoped_interpreter guard;
try {
py::exec(R"(
import numpy
print("module loaded")
)");
} catch (py::error_already_set &e) {
std::cerr << e.what() << "\n";
}
}
int main() {
// py::scoped_interpreter guard; // If the `guard` is in the `main` scope, there is no issue.
test();
test(); // this call fails to import numpy
return 0;
}
All the python parts happen inside a function with a scoped_interpreter. I call the function twice from main. The first time, everything works. The second time it fails. And this is only if numpy is imported. Output error is different in different version of python and numpy. But I think it is related to numpy's CPU dispatcher.
P.S. I had created a discussion when I thought that I was doing something wrong. But, now I'm more inclined to believe that this is an issue.
Originally posted by @nkr0 in https://github.com/pybind/pybind11/discussions/5171
I've this
c++
code. It hasimport numpy
in a function which is called twice.All the
python
parts happen inside a function with ascoped_interpreter
. I call the function twice frommain
. The first time, everything works. The second time it fails. And this is only ifnumpy
is imported. Output error is different in different version ofpython
andnumpy
. But I think it is related tonumpy
's CPU dispatcher.P.S. I had created a discussion when I thought that I was doing something wrong. But, now I'm more inclined to believe that this is an issue. Originally posted by @nkr0 in https://github.com/pybind/pybind11/discussions/5171