Skip to content

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

Closed

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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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

Activity

  1. jiwaszki commented on Jul 14, 2021

    @jiwaszki
    Contributor

    Would be nice having some compilation instructions for your example and minimal code for C++ (for example your is missing includes and } ). Are you binding test() to some python module or as standalone cpp?

    Maybe you could look at #1702 . For me using gil_scoped_acquire() when biding to module generally resolves issues.

  2. MartinPerry commented on Jul 16, 2021

    @MartinPerry
    Author

    I have found out that the problem is not pybind but numpy that cannot be reinitialized. I have solved it by initializing pybind interpreter once for entire program.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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