Behavior of multi-phase initialized modules on re-import after sys.modules
reset (pybind11 v3)
#5780
Replies: 2 comments 2 replies
-
I didn't know that At any rate, I wouldn't say that this behavior was expected (by me) or intentional. I don't know of a way to trigger the exact same behavior (completely unloading the module) while using multi-phase init. It doesn't seem possible. We could work around it with a few hacky options (like storing the module reference somewhere and returning the same one if called again). The easiest solution is, as you suggest, putting an attribute or something and telling anyone trying to do this to check that attribute instead. Since I suspect doing something like this is kind of rare... |
Beta Was this translation helpful? Give feedback.
-
#5782 was merged. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
I'm trying to understand the correct behavior and expectations for pybind11 v3.0.0+ modules that use multi-phase initialization (PEP 489). After upgrading from v2.x to v3.0.0, if I import a pybind11 module (e.g.,
mycode
), then manually delete it fromsys.modules
and import it again, I get the following error:This didn't happen under pybind11 v2.x (single-phase init), but now occurs consistently in v3.0.0. Here's a minimal reproducer:
For context, this issue surfaced while using the pytest's pytester plugin (which snapshots and restores
sys.modules
between test runs) to test Matplotlib's pytest plugin (matplotlib/pytest-mpl#248). Future releases of Matplotlib will be built with pybind11>=3, and we are running into issues withimport matplotlib
across multiple tests in pytest-mpl.__pybind11_version__
or__pybind11_multi_phase__
attribute help downstream tools like pytest detect modules that shouldn't be deleted from sys.modules? Or maybe there is already a way? Longer term, maybe pytester should use subinterpreters instead of resettingsys.modules
. It does have a subprocess mode, but I'd rather avoid it due to its speed.The behavior seems directly related to the multi-phase init work in #5574, so tagging @b-pass for possible input – hope that's okay!
Thanks for the great work on pybind11!
Beta Was this translation helpful? Give feedback.
All reactions