I don’t know why I didn’t notice this before.
$ python
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 sys
>>> len(sys.modules.keys())
38
>>> raise Exception
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Exception
>>> len(sys.modules.keys())
378
>>>
After raising the exception, sys.modules
has 340 new entries. Seems like it might be the entire dist-packages
folder, which apparently includes things like requests
.
It doesn’t happen with Python that I built myself from source:
$ 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 sys
>>> len(sys.modules.keys())
70
>>> raise Exception
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Exception
>>> len(sys.modules.keys())
70
>>>
Nor with a virtual environment based off the system Python:
$ source SANDBOX/bin/activate
$ python
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 sys
>>> len(sys.modules.keys())
61
>>> raise Exception
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Exception
>>> len(sys.modules.keys())
61
>>>
Is there a known, general reason for this sort of thing to happen? Or do I have an OS support question instead?