Skip to content

[QUESTION] Import module second time fails with numpy #3112

@MartinPerry

Description

@MartinPerry

I use Pybind11 and import module

void test(){
   py::scoped_interpreter python{};
   try {
       py::module pyModule = py::module_::import("example");
       //... use module
   }
   catch (py::error_already_set const &pythonErr) {  
      std::cout << pythonErr.what();
   }
}

example.py

import numpy as np
a = 1
b = a + 1
print(b)

However, when I call the method test second time, it raises the exception:

SystemError: Objects/structseq.c:401: bad argument to internal function

At:
  /usr/local/opt/python@3.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/numpy/core/overrides.py(7): <module>
  <frozen importlib._bootstrap>(228): _call_with_frames_removed
  <frozen importlib._bootstrap_external>(850): exec_module
  <frozen importlib._bootstrap>(695): _load_unlocked
  <frozen importlib._bootstrap>(986): _find_and_load_unlocked
  <frozen importlib._bootstrap>(1007): _find_and_load
  <frozen importlib._bootstrap>(228): _call_with_frames_removed
  <frozen importlib._bootstrap>(1066): _handle_fromlist
  /usr/local/opt/python@3.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/numpy/core/multiarray.py(12): <module>
  <frozen importlib._bootstrap>(228): _call_with_frames_removed
  <frozen importlib._bootstrap_external>(850): exec_module
  <frozen importlib._bootstrap>(695): _load_unlocked
  <frozen importlib._bootstrap>(986): _find_and_load_unlocked
  <frozen importlib._bootstrap>(1007): _find_and_load
  <frozen importlib._bootstrap>(228): _call_with_frames_removed
  <frozen importlib._bootstrap>(1066): _handle_fromlist
  /usr/local/opt/python@3.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/numpy/core/__init__.py(51): <module>
  <frozen importlib._bootstrap>(228): _call_with_frames_removed
  <frozen importlib._bootstrap_external>(850): exec_module
  <frozen importlib._bootstrap>(695): _load_unlocked
  <frozen importlib._bootstrap>(986): _find_and_load_unlocked
  <frozen importlib._bootstrap>(1007): _find_and_load
  <frozen importlib._bootstrap>(228): _call_with_frames_removed
  <frozen importlib._bootstrap>(1066): _handle_fromlist
  /usr/local/opt/python@3.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/numpy/__init__.py(150): <module>
  <frozen importlib._bootstrap>(228): _call_with_frames_removed
  <frozen importlib._bootstrap_external>(850): exec_module
  <frozen importlib._bootstrap>(695): _load_unlocked
  <frozen importlib._bootstrap>(986): _find_and_load_unlocked
  <frozen importlib._bootstrap>(1007): _find_and_load example.py(3): <module>
  <frozen importlib._bootstrap>(228): _call_with_frames_removed
  <frozen importlib._bootstrap_external>(850): exec_module
  <frozen importlib._bootstrap>(695): _load_unlocked
  <frozen importlib._bootstrap>(986): _find_and_load_unlocked
  <frozen importlib._bootstrap>(1007): _find_and_load

If I remove

import numpy as np

it is working.

I am using MacOS BigSur 11.2.3

Note: This is a simple example where numpy is not used, but in my actual code I want to use it

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions