-
-
Notifications
You must be signed in to change notification settings - Fork 12.2k
Description
Describe the issue:
As of numpy 1.22.0, specifically after 1b5b587, import numpy.testing implicitly imports numpy.distutils. The exact line that introduced this is 84e0707#diff-657b0df76a18e77d5033e7a763d2e40a58498dee25374a90c4db9850ce2f75d3R13 which appears to load an extension module building helper (utilising numpy.distutils) into the numpy.testing namespace. This is unfortunate because, despite numpy's own usages of numpy.testing being avoided except when testing, downstream projects do still use it. For example import scipy imports numpy.testing (somewhat unnecessarily IMO but I'll make that case on their issue tracker) which now drags in numpy.distutils.
Reproduce the code example:
# Attempt to load numpy.testing after blocking imports of `numpy.distutils`.
import sys
sys.modules["numpy.distutils"] = None
import numpy.testing
# Alternatively, you can skip the sys.modules blocking and just run:
import numpy.testing
assert "numpy.distutils" not in sys.modules
# But the first provides a traceback which indicates where the import leak is.Error message:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/brenainn/.local/lib/python3.9/site-packages/numpy/testing/__init__.py", line 12, in <module>
from ._private import extbuild, decorators as dec
File "/home/brenainn/.local/lib/python3.9/site-packages/numpy/testing/_private/extbuild.py", line 11, in <module>
from numpy.distutils.ccompiler import new_compiler
ModuleNotFoundError: No module named 'numpy.distutils.ccompiler'; 'numpy.distutils' is not a packageNumPy/Python version information:
1.22.0 3.9.6 (default, Jul 30 2021, 22:48:59)
[GCC 11.1.0]
This is specific to numpy 1.22.0. Earlier versions of numpy did not do this.
Note to whoever addresses this: If this is fixed after #20745 is merged then this conditional statement may be made unconditional.