Python3.11 - importlib no longer exposes .util

Had this issue yesterday that actually created that behavior. I was able to trackdown down the package that was implicitly import the util module.

Installing matplotlib 3.7.5 or lower caused the import importlib; importlib.util to import importlib.util in a Frozen state, but it would work regardless.

I think it might be fairly common for the data science folks to always have matplotlib installed, so we probably assumed that importing importlib would also import util module by default and that is why it might have been under the radar for a while now. Installing a newer matplotlib such as 3.8 seems to have the intended behavior already.

I can’t reproduce any meaningful Python-specific differences.

$ python3.8
Python 3.8.10 (default, Nov 22 2023, 10:22:35) 
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import importlib
>>> importlib.util
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'importlib' has no attribute 'util'
>>> import importlib.util
>>> 
$ python3.11
Python 3.11.2 (main, Apr  5 2023, 03:08:14) [GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import importlib
>>> importlib.util
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'importlib' has no attribute 'util'
>>> import importlib.util
>>> 
$ python3.12
Python 3.12.2 (main, Mar 29 2024, 00:26:08) [GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import importlib
>>> importlib.util
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'importlib' has no attribute 'util'
>>> import importlib.util
>>> 
$ python3.8
Python 3.8.10 (default, Nov 22 2023, 10:22:35) 
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pkgutil
>>> importlib.util
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'importlib' is not defined
>>> import importlib
>>> importlib.util
<module 'importlib.util' from '/usr/lib/python3.8/importlib/util.py'>
>>> 
$ python3.11
Python 3.11.2 (main, Apr  5 2023, 03:08:14) [GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pkgutil
>>> importlib.util
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'importlib' is not defined
>>> import importlib
>>> importlib.util
<module 'importlib.util' (frozen)>
>>> 
$ python3.12
Python 3.12.2 (main, Mar 29 2024, 00:26:08) [GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pkgutil
>>> importlib.util
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'importlib' is not defined. Did you forget to import 'importlib'?
>>> import importlib
>>> importlib.util
<module 'importlib.util' (frozen)>
>>> 

The fact that an imported module is “frozen” is not really relevant here; it’s an implementation detail and doesn’t change how you use it.