You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Sphinx 7.2.5 breaks with PyO3: "modules may only be initialized once per interpreter process" #11662
New error message with this patch release. 7.2.4 succeeds in building the same documents. PyO3 modules may only be initialized once per interpreter process
Full traceback
Traceback (most recent call last):
File ".venv/lib/python3.10/site-packages/sphinx/cmd/build.py", line 298, in build_main
app.build(args.force_all, args.filenames)
File ".venv/lib/python3.10/site-packages/sphinx/application.py", line 351, in build
self.builder.build_all()
File ".venv/lib/python3.10/site-packages/sphinx/builders/__init__.py", line 252, in build_all
self.build(None, summary=__('all source files'), method='all')
File ".venv/lib/python3.10/site-packages/sphinx/builders/__init__.py", line 312, in build
with logging.pending_warnings():
File "/usr/lib/python3.10/contextlib.py", line 142, in __exit__
next(self.gen)
File ".venv/lib/python3.10/site-packages/sphinx/util/logging.py", line 222, in pending_warnings
memhandler.flushTo(logger)
File ".venv/lib/python3.10/site-packages/sphinx/util/logging.py", line 187, in flushTo
logger.handle(record)
File "/usr/lib/python3.10/logging/__init__.py", line 1634, in handle
self.callHandlers(record)
File "/usr/lib/python3.10/logging/__init__.py", line 1696, in callHandlers
hdlr.handle(record)
File "/usr/lib/python3.10/logging/__init__.py", line 964, in handle
rv = self.filter(record)
File "/usr/lib/python3.10/logging/__init__.py", line 821, in filter
result = f.filter(record)
File ".venv/lib/python3.10/site-packages/sphinx/util/logging.py", line 427, in filter
raise exc
sphinx.errors.SphinxWarning: autodoc: failed to import module 'test' from module 'test'; the following exception was raised:
PyO3 modules may only be initialized once per interpreter process
How to Reproduce
install latest sphinx and run sphinx-build -WaE docs/project docs/project/_build/html
@pbybee Please provide a full reproducer. PyO3 is for rust projects, which you haven't mentioned in your description. Does the error appear with Sphinx 7.2.4?
changed the title [-]patch release 7.2.5 breaks document builds with PyO3 modules may only be initialized once per interpreter process[/-][+]Sphinx 7.2.5 breaks with PyO3: "modules may only be initialized once per interpreter process"[/+]on Aug 30, 2023
No 7.2.4 builds correctly. We are not using PyO3 in our project. Perhaps it's a dependancy of sphinx that revved. I'll dig a little more as I don't have good repro steps at the moment without digging through the sphinx build process.
Can you check if the bad module contains a TYPE_CHECKING block and if any of the imports in this block contains some PyO3 object somewhere (or depends on it)? Could you also give us the dependencies of your project?
As Adam pointed out in another issue where the feature of reloading a module with TYPE_CHECKING enabled makes jax fails, can you check the bad module with pyanalyze?
@AA-Turner I am wondering, but for projects that would fail like that, could we perhaps make the TYPE_CHECKING feature optional? (it's probably impossible to make it optional per module due to dependencies that we would then need to build in advance).
Assume that you have a module using PyO3 say yay and a bad module boo
Assume you have a module a resp. b) importing yay (resp. yay and boo) in a TYPE_CHECKING block.
Import a: success for yay, failure for boo. We remove yay from sys.modules (the commit Adam referred to).
Import b: failure for yayeven ifsys.modules does not contain yay.
Reason: PyO3 seems to import modules but does not seem to "unimport" them if they are deleted from sys.modules. As such, it's probably impossible for us to do anything about that.
EDIT: I concluded as such by creating a module using PyO3, importing it, deleting it from.sys.modules, and re-importing it again and here is my configuration:
This may not apply exclusively to PyO3. We are seeing a similar issue with importing the onnx package (which is built using Pybind11).
WARNING: autodoc: failed to import module 'onnx.testing' from module <module-name>; the following exception was raised:
Traceback (most recent call last):
File "/opt/conda/envs/env-name/lib/python3.9/site-packages/sphinx/ext/autodoc/importer.py", line 65, in import_module
return importlib.import_module(modname)
File "/opt/conda/envs/env-name/lib/python3.9/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
Actually, it's probably something related to how extension modules are imported. It appears that deleting them from sys.modules is not sufficient. By the way, what is the full traceback error? you are missing the exception message (or there is none?)
Mmh thank you. As I only have my phone for investing this, it will be hard. In the meantime, maybe the issue could be directly related to PyO3? Or, alternatively, for extension modules, maybe we could skip their removal, though they would still be leftovers modules... So I don't really know what to do (except pinning the version prior to 7.1.x. I wouldn't recommend 7.2.x as this is kind of subject to some unstability).
importimportlib, sysorig_modules=frozenset(sys.modules)
module=importlib.import_module('pydantic')
formin [mforminsys.modulesifmnotinorig_modules]:
sys.modules.pop(m)
module=importlib.import_module('pydantic')
# ImportError: PyO3 modules may only be initialized once per interpreter process
cc: @davidhewitt -- sorry for the ping, but I wondered if you'd have any advice here? The problem is Sphinx tries to import modules twice (with TYPE_CHECKING set to True and then False on failure), but wants to reset sys.modules to the initial state in the case of a failed import, to work around corrupted partial imports (#11645).
Is there an officially blessed way to unload a PyO3 module such that we're able to attempt importing it again? (Other ideas to work around this are very welcome also, I've not had much luck finding discussion on this point).
I have something that looks similar to this as well in https://github.com/lab-cosmo/metatensor. The docs builds fine with sphinx 7.2.4, and fails with 7.2.5 with a numpy error that looks related to typechecking: numpy.core._exceptions._UFuncNoLoopError: ufunc 'equal' did not contain a loop with signature matching types (<class 'numpy.dtypes.Float64DType'>, <class 'numpy.dtypes.StrDType'>) -> None (full error below).
Details
sphinx-build -E -W -b html docs/src docs/build/html
Sphinx v7.2.5 en cours d'exécution
<metatensor>/.tox/docs/lib/python3.11/site-packages/breathe/project.py:116: RemovedInSphinx80Warning: Sphinx 8 will drop support for representing paths as strings. Use "pathlib.Path" or "os.fspath" instead.
self._default_build_dir = os.path.dirname(app.doctreedir.rstrip(os.sep))
Finished dev [unoptimized + debuginfo] target(s) in 0.05s
chargement de l'inventaire intersphinx de https://docs.python.org/3/objects.inv...
chargement de l'inventaire intersphinx de https://numpy.org/doc/stable/objects.inv...
chargement de l'inventaire intersphinx de https://pytorch.org/docs/stable/objects.inv...
Construction en cours [mo] : cibles périmées pour les fichiers po 0
Écriture...
Construction [html] : cibles périmées pour les fichiers sources 77
Mise à jour de l'environnement : [nouvelle configuration] 77 ajouté(s), 0 modifié(s), 0 supprimé(s)
Lecture des sources... [100%] reference/torch/tensor
Warning, treated as error:
autodoc: failed to import function 'checks_enabled' from module 'metatensor'; the following exception was raised:
Traceback (most recent call last):
File "<metatensor>/.tox/docs/lib/python3.11/site-packages/sphinx/ext/autodoc/importer.py", line 65, in import_module
return importlib.import_module(modname)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<miniforge3>/lib/python3.11/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 940, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "<metatensor>/.tox/docs/lib/python3.11/site-packages/metatensor/__init__.py", line 28, in <module>
from . import core # noqa
^^^^^^^^^^^^^^^^^^
File "<metatensor>/.tox/docs/lib/python3.11/site-packages/metatensor/core/__init__.py", line 2, in <module>
from .block import TensorBlock
File "<metatensor>/.tox/docs/lib/python3.11/site-packages/metatensor/core/block.py", line 6, in <module>
from ._c_lib import _get_library
File "<metatensor>/.tox/docs/lib/python3.11/site-packages/metatensor/core/_c_lib.py", line 8, in <module>
from .data.extract import ExternalCpuArray, register_external_data_wrapper
File "<metatensor>/.tox/docs/lib/python3.11/site-packages/metatensor/core/data/__init__.py", line 1, in <module>
from .array import ArrayWrapper # noqa
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<metatensor>/.tox/docs/lib/python3.11/site-packages/metatensor/core/data/array.py", line 3, in <module>
import numpy as np
File "<metatensor>/.tox/docs/lib/python3.11/site-packages/numpy/__init__.py", line 144, in <module>
from . import lib
File "<metatensor>/.tox/docs/lib/python3.11/site-packages/numpy/lib/__init__.py", line 25, in <module>
from . import index_tricks
File "<metatensor>/.tox/docs/lib/python3.11/site-packages/numpy/lib/index_tricks.py", line 12, in <module>
import numpy.matrixlib as matrixlib
File "<metatensor>/.tox/docs/lib/python3.11/site-packages/numpy/matrixlib/__init__.py", line 4, in <module>
from . import defmatrix
File "<metatensor>/.tox/docs/lib/python3.11/site-packages/numpy/matrixlib/defmatrix.py", line 12, in <module>
from numpy.linalg import matrix_power
File "<metatensor>/.tox/docs/lib/python3.11/site-packages/numpy/linalg/__init__.py", line 73, in <module>
from . import linalg
File "<metatensor>/.tox/docs/lib/python3.11/site-packages/numpy/linalg/linalg.py", line 37, in <module>
from numpy._typing import NDArray
File "<metatensor>/.tox/docs/lib/python3.11/site-packages/numpy/_typing/__init__.py", line 207, in <module>
from ._ufunc import (
ModuleNotFoundError: No module named 'numpy._typing._ufunc'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<metatensor>/.tox/docs/lib/python3.11/site-packages/sphinx/ext/autodoc/importer.py", line 90, in import_object
module = import_module(modname, warningiserror=warningiserror)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<metatensor>/.tox/docs/lib/python3.11/site-packages/sphinx/ext/autodoc/importer.py", line 69, in import_module
raise ImportError(exc, traceback.format_exc()) from exc
ImportError: (ModuleNotFoundError("No module named 'numpy._typing._ufunc'"), 'Traceback (most recent call last):\n File "<metatensor>/.tox/docs/lib/python3.11/site-packages/sphinx/ext/autodoc/importer.py", line 65, in import_module\n return importlib.import_module(modname)\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File "<miniforge3>/lib/python3.11/importlib/__init__.py", line 126, in import_module\n return _bootstrap._gcd_import(name[level:], package, level)\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File "<frozen importlib._bootstrap>", line 1204, in _gcd_import\n File "<frozen importlib._bootstrap>", line 1176, in _find_and_load\n File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked\n File "<frozen importlib._bootstrap>", line 690, in _load_unlocked\n File "<frozen importlib._bootstrap_external>", line 940, in exec_module\n File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed\n File "<metatensor>/.tox/docs/lib/python3.11/site-packages/metatensor/__init__.py", line 28, in <module>\n from . import core # noqa\n ^^^^^^^^^^^^^^^^^^\n File "<metatensor>/.tox/docs/lib/python3.11/site-packages/metatensor/core/__init__.py", line 2, in <module>\n from .block import TensorBlock\n File "<metatensor>/.tox/docs/lib/python3.11/site-packages/metatensor/core/block.py", line 6, in <module>\n from ._c_lib import _get_library\n File "<metatensor>/.tox/docs/lib/python3.11/site-packages/metatensor/core/_c_lib.py", line 8, in <module>\n from .data.extract import ExternalCpuArray, register_external_data_wrapper\n File "<metatensor>/.tox/docs/lib/python3.11/site-packages/metatensor/core/data/__init__.py", line 1, in <module>\n from .array import ArrayWrapper # noqa\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File "<metatensor>/.tox/docs/lib/python3.11/site-packages/metatensor/core/data/array.py", line 3, in <module>\n import numpy as np\n File "<metatensor>/.tox/docs/lib/python3.11/site-packages/numpy/__init__.py", line 144, in <module>\n from . import lib\n File "<metatensor>/.tox/docs/lib/python3.11/site-packages/numpy/lib/__init__.py", line 25, in <module>\n from . import index_tricks\n File "<metatensor>/.tox/docs/lib/python3.11/site-packages/numpy/lib/index_tricks.py", line 12, in <module>\n import numpy.matrixlib as matrixlib\n File "<metatensor>/.tox/docs/lib/python3.11/site-packages/numpy/matrixlib/__init__.py", line 4, in <module>\n from . import defmatrix\n File "<metatensor>/.tox/docs/lib/python3.11/site-packages/numpy/matrixlib/defmatrix.py", line 12, in <module>\n from numpy.linalg import matrix_power\n File "<metatensor>/.tox/docs/lib/python3.11/site-packages/numpy/linalg/__init__.py", line 73, in <module>\n from . import linalg\n File "<metatensor>/.tox/docs/lib/python3.11/site-packages/numpy/linalg/linalg.py", line 37, in <module>\n from numpy._typing import NDArray\n File "<metatensor>/.tox/docs/lib/python3.11/site-packages/numpy/_typing/__init__.py", line 207, in <module>\n from ._ufunc import (\nModuleNotFoundError: No module named \'numpy._typing._ufunc\'\n')
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<metatensor>/.tox/docs/lib/python3.11/site-packages/sphinx/ext/autodoc/importer.py", line 65, in import_module
return importlib.import_module(modname)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<miniforge3>/lib/python3.11/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 940, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "<metatensor>/.tox/docs/lib/python3.11/site-packages/metatensor/__init__.py", line 28, in <module>
from . import core # noqa
^^^^^^^^^^^^^^^^^^
File "<metatensor>/.tox/docs/lib/python3.11/site-packages/metatensor/core/__init__.py", line 2, in <module>
from .block import TensorBlock
File "<metatensor>/.tox/docs/lib/python3.11/site-packages/metatensor/core/block.py", line 6, in <module>
from ._c_lib import _get_library
File "<metatensor>/.tox/docs/lib/python3.11/site-packages/metatensor/core/_c_lib.py", line 8, in <module>
from .data.extract import ExternalCpuArray, register_external_data_wrapper
File "<metatensor>/.tox/docs/lib/python3.11/site-packages/metatensor/core/data/__init__.py", line 1, in <module>
from .array import ArrayWrapper # noqa
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<metatensor>/.tox/docs/lib/python3.11/site-packages/metatensor/core/data/array.py", line 3, in <module>
import numpy as np
File "<metatensor>/.tox/docs/lib/python3.11/site-packages/numpy/__init__.py", line 387, in <module>
_mac_os_check()
File "<metatensor>/.tox/docs/lib/python3.11/site-packages/numpy/__init__.py", line 381, in _mac_os_check
_ = polyfit(x, y, 2, cov=True)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<metatensor>/.tox/docs/lib/python3.11/site-packages/numpy/lib/polynomial.py", line 669, in polyfit
c, resids, rank, s = lstsq(lhs, rhs, rcond)
^^^^^^^^^^^^^^^^^^^^^^
File "<metatensor>/.tox/docs/lib/python3.11/site-packages/numpy/linalg/linalg.py", line 2303, in lstsq
if rcond == "warn":
^^^^^^^^^^^^^^^
numpy.core._exceptions._UFuncNoLoopError: ufunc 'equal' did not contain a loop with signature matching types (<class 'numpy.dtypes.Float64DType'>, <class 'numpy.dtypes.StrDType'>) -> None
EDIT: we are only seeing this because we build docs with sphinx-build -W, other people might be affected but ignoring the warning.
Well, #11679 is an implementation of my second idea for how to make this work, leveraging importlib.reload().
I'm not convinced that any of this is a good idea, and I think that leaving typing.TYPE_CHECKING alone is a better idea - or at the very least, making it opt-in for Sphinx to mess with it. TYPE_CHECKING is explicitly meant for static analysis, and setting it at runtime seems to just be asking for trouble.
But, this new PR does seem to work in more cases than #11645 did. It handles pydantic, at least (I tested that locally, rather than introducing a new test dependency on pydantic). It also passes the tests we added for #11645. I think it's also more likely to leave things in a valid state if the reload fails, by virtue of loading with TYPE_CHECKING == False first, so that there's something valid already in sys.modules that we can leverage if reloading with TYPE_CHECKING == True fails.
@inducer Would you be able to test #11679 with grudge by any chance? I tried to reproduce with pip install grudge and .. automodule:: grudge but didn't get a failure even with Sphinx 7.2.5
@inducer Would you be able to test #11679 with grudge by any chance? I tried to reproduce with pip install grudge and .. automodule:: grudge but didn't get a failure even with Sphinx 7.2.5
The grudge doc build still fails with #11679, with a new error message:
WARNING: autodoc: failed to import function 'op.elementwise_min' from module 'grudge'; the following exception was raised:
Traceback (most recent call last):
File "/Users/mdiener/Work/emirge/sphinx/sphinx/ext/autodoc/importer.py", line 66, in import_module
return importlib.import_module(modname)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/mdiener/Work/emirge/miniforge3/envs/ceesd/lib/python3.11/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 940, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "/Users/mdiener/Work/emirge/grudge/grudge/op.py", line 115, in <module>
from grudge.trace_pair import (
File "/Users/mdiener/Work/emirge/grudge/grudge/trace_pair.py", line 95, in <module>
@dataclass_array_container
^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/mdiener/Work/emirge/arraycontext/arraycontext/container/dataclass.py", line 117, in dataclass_array_container
raise ValueError(f"'{cls}' must have fields with array container type "
ValueError: '<class 'grudge.trace_pair.TracePair'>' must have fields with array container type in order to use the 'dataclass_array_container' decorator
WARNING: autodoc: failed to import module 'grudge'; the following exception was raised:
cannot import name 'BoundaryTag' from 'meshmode.mesh' (/home/godlygeek/3ps/sphinx/venv/lib/python3.11/site-packages/meshmode/mesh/__init__.py)
That's with meshmode 2021.2, and grudge's requirement is meshmode>=2020.2, so it ought to be compatible...
And it works with Sphinx 7.2.4? That's weird. Hm. Can you give me a recipe I can follow to reproduce that failure myself?
Yes, it works with sphinx 7.2.4. I think the easiest way to install grudge and the correct dependencies might be via emirge:
git clone https://github.com/illinois-ceesd/emirge
cd emirge
./install.sh
source config/activate_env.sh
pip install matplotlib
cd grudge/doc
make html
Well, it looks like the issue with grudge is that it's using matplotlib.sphinxext.plot_directive, which does execute code from the module being documented. It does so after we've reloaded those modules, and some of the modules in this program don't take kindly to being reloaded.
One of my assumptions with the "use importlib.reload" idea was that Sphinx wasn't going to actually try to call functions from the module, but matplotlib.sphinxext.plot_directive does.
So... I'm not sure I have any good ideas at this point. I'm not seeing any reasonable way to import modules with typing.TYPE_CHECKING set that isn't going to badly break some things.
For the sake of anyone stumbling upon this issue, it might be helpful to document that there's a reasonable workaround for both the problems caused by 7.2.0 and the different problems caused by 7.2.5: if you import the modules that you're trying to document yourself from inside your conf.py file, neither the 7.2.0 version of the TYPE_CHECKING reimport nor the 7.2.5 version will trigger. The already-loaded module will just get picked up from sys.modules with no additional magic.
Activity
AA-Turner commentedon Aug 30, 2023
@pbybee Please provide a full reproducer. PyO3 is for rust projects, which you haven't mentioned in your description. Does the error appear with Sphinx 7.2.4?
A
[-]patch release 7.2.5 breaks document builds with PyO3 modules may only be initialized once per interpreter process[/-][+]Sphinx 7.2.5 breaks with PyO3: "modules may only be initialized once per interpreter process"[/+]pbybee commentedon Aug 30, 2023
No 7.2.4 builds correctly. We are not using PyO3 in our project. Perhaps it's a dependancy of sphinx that revved. I'll dig a little more as I don't have good repro steps at the moment without digging through the sphinx build process.
AA-Turner commentedon Aug 30, 2023
If you're able to bisect, try before/after this commit: 8248be3
AA-Turner commentedon Aug 30, 2023
Very unlikely, more likely is that a dependency of the object you're documenting uses PyO3.
picnixz commentedon Aug 31, 2023
Can you check if the bad module contains a TYPE_CHECKING block and if any of the imports in this block contains some PyO3 object somewhere (or depends on it)? Could you also give us the dependencies of your project?
As Adam pointed out in another issue where the feature of reloading a module with TYPE_CHECKING enabled makes jax fails, can you check the bad module with
pyanalyze?@AA-Turner I am wondering, but for projects that would fail like that, could we perhaps make the TYPE_CHECKING feature optional? (it's probably impossible to make it optional per module due to dependencies that we would then need to build in advance).
picnixz commentedon Aug 31, 2023
Ok I know what happened.
yayand a bad modulebooaresp.b) importingyay(resp.yayandboo) in a TYPE_CHECKING block.a: success foryay, failure forboo. We removeyayfromsys.modules(the commit Adam referred to).b: failure foryayeven ifsys.modulesdoes not containyay.Reason: PyO3 seems to import modules but does not seem to "unimport" them if they are deleted from
sys.modules. As such, it's probably impossible for us to do anything about that.EDIT: I concluded as such by creating a module using PyO3, importing it, deleting it from.
sys.modules, and re-importing it again and here is my configuration:adityagoel4512 commentedon Aug 31, 2023
This may not apply exclusively to PyO3. We are seeing a similar issue with importing the onnx package (which is built using Pybind11).
This error is not present in 7.2.4.
picnixz commentedon Aug 31, 2023
Actually, it's probably something related to how extension modules are imported. It appears that deleting them from sys.modules is not sufficient. By the way, what is the full traceback error? you are missing the exception message (or there is none?)
jaraco commentedon Aug 31, 2023
I've also encountered this issue in jaraco/skeleton#88 and I've produced a minimal reproducer in https://github.com/jaraco/jaraco.mongodb/tree/triage/36-minimal. Run with
tox -e docs.In this case, the offending import is
import inflect, which has a dependency onpydantic, which has a rust extension.picnixz commentedon Aug 31, 2023
Mmh thank you. As I only have my phone for investing this, it will be hard. In the meantime, maybe the issue could be directly related to PyO3? Or, alternatively, for extension modules, maybe we could skip their removal, though they would still be leftovers modules... So I don't really know what to do (except pinning the version prior to 7.1.x. I wouldn't recommend 7.2.x as this is kind of subject to some unstability).
jaraco commentedon Aug 31, 2023
I installed the commit prior to that one and the issue remains.Correction - after ensuring that I'm forcing the reinstall, I have confirmed that commit is implicated:
AA-Turner commentedon Sep 1, 2023
cc @godlygeek as the author of #11645 if you've any ideas re PyO3/extension module unloading in general.
AA-Turner commentedon Sep 1, 2023
Another simple reproducer:
AA-Turner commentedon Sep 1, 2023
See also https://github.com/PyO3/pyo3/blob/v0.19.2/guide/src/migration.md#each-pymodule-can-now-only-be-initialized-once-per-process
cc: @davidhewitt -- sorry for the ping, but I wondered if you'd have any advice here? The problem is Sphinx tries to import modules twice (with
TYPE_CHECKINGset toTrueand thenFalseon failure), but wants to resetsys.modulesto the initial state in the case of a failed import, to work around corrupted partial imports (#11645).Is there an officially blessed way to unload a PyO3 module such that we're able to attempt importing it again? (Other ideas to work around this are very welcome also, I've not had much luck finding discussion on this point).
Thanks,
Adam
18 remaining items
Luthaf commentedon Sep 7, 2023
I have something that looks similar to this as well in https://github.com/lab-cosmo/metatensor. The docs builds fine with sphinx 7.2.4, and fails with 7.2.5 with a numpy error that looks related to typechecking:
numpy.core._exceptions._UFuncNoLoopError: ufunc 'equal' did not contain a loop with signature matching types (<class 'numpy.dtypes.Float64DType'>, <class 'numpy.dtypes.StrDType'>) -> None(full error below).Details
EDIT: we are only seeing this because we build docs with
sphinx-build -W, other people might be affected but ignoring the warning.godlygeek commentedon Sep 7, 2023
Well, #11679 is an implementation of my second idea for how to make this work, leveraging
importlib.reload().I'm not convinced that any of this is a good idea, and I think that leaving
typing.TYPE_CHECKINGalone is a better idea - or at the very least, making it opt-in for Sphinx to mess with it.TYPE_CHECKINGis explicitly meant for static analysis, and setting it at runtime seems to just be asking for trouble.But, this new PR does seem to work in more cases than #11645 did. It handles pydantic, at least (I tested that locally, rather than introducing a new test dependency on pydantic). It also passes the tests we added for #11645. I think it's also more likely to leave things in a valid state if the reload fails, by virtue of loading with
TYPE_CHECKING == Falsefirst, so that there's something valid already insys.modulesthat we can leverage if reloading withTYPE_CHECKING == Truefails.godlygeek commentedon Sep 7, 2023
@inducer Would you be able to test #11679 with
grudgeby any chance? I tried to reproduce withpip install grudgeand.. automodule:: grudgebut didn't get a failure even with Sphinx 7.2.5matthiasdiener commentedon Sep 7, 2023
The grudge doc build still fails with #11679, with a new error message:
godlygeek commentedon Sep 7, 2023
And it works with Sphinx 7.2.4? That's weird. Hm. Can you give me a recipe I can follow to reproduce that failure myself?
I tried cloning https://github.com/inducer/grudge.git and doing a
pip install path/to/clone, but things failed for me entirely differently than for you:That's with meshmode 2021.2, and grudge's requirement is
meshmode>=2020.2, so it ought to be compatible...matthiasdiener commentedon Sep 7, 2023
Yes, it works with sphinx 7.2.4. I think the easiest way to install grudge and the correct dependencies might be via emirge:
picnixz commentedon Sep 7, 2023
Concerning the reason why we had this bug, I suggested the following: #11652 (comment)
godlygeek commentedon Sep 8, 2023
Well, it looks like the issue with
grudgeis that it's usingmatplotlib.sphinxext.plot_directive, which does execute code from the module being documented. It does so after we've reloaded those modules, and some of the modules in this program don't take kindly to being reloaded.One of my assumptions with the "use
importlib.reload" idea was that Sphinx wasn't going to actually try to call functions from the module, butmatplotlib.sphinxext.plot_directivedoes.So... I'm not sure I have any good ideas at this point. I'm not seeing any reasonable way to import modules with
typing.TYPE_CHECKINGset that isn't going to badly break some things.For the sake of anyone stumbling upon this issue, it might be helpful to document that there's a reasonable workaround for both the problems caused by 7.2.0 and the different problems caused by 7.2.5: if you import the modules that you're trying to document yourself from inside your
conf.pyfile, neither the 7.2.0 version of theTYPE_CHECKINGreimport nor the 7.2.5 version will trigger. The already-loaded module will just get picked up fromsys.moduleswith no additional magic.davidhewitt commentedon Sep 11, 2023
Posted PyO3/pyo3#3446 - I'll try to ship this within a week and update Pydantic to use.
Evade a Sphinx/PyO3 bug (temporarily) in docs builds.
picnixz commentedon Sep 21, 2023
@pbybee With #11679 merged (7.2.6), I think your issue should not persist anymore. Could you confirm?
Luthaf commentedon Sep 21, 2023
7.2.6 does fix the issue in my case (#11662 (comment)). Thanks a lot to all contributors!
pbybee commentedon Sep 22, 2023
Confirmed fixed in 7.2.6. Thanks for the hard work on this!