Skip to content

[BUG]: Runtime error loading numpy #5173

@nkr0

Description

@nkr0

Required prerequisites

What version (or hash if on master) of pybind11 are you using?

2.12.0

Problem description

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 versions of python and numpy. But I think it is related to numpy's CPU dispatcher.

Reproducible example code

#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;
}

Is this a regression? Put the last known working version here if it is.

Not a regression

Metadata

Metadata

Assignees

No one assigned

    Labels

    triageNew bug, unverified

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions