Closed
Description
Currently, when a test is run by ./python -m test (...)
, the test starts with 170 modules already imported. The problem is that imports can have side effects and I would prefer to start tests in a "minimal environment".
Running a test with ./python -m test (...)
only imports 164 modules.
Example by adding Lib/test/test_x.py
:
import sys
print(len(sys.modules))
Output:
vstinner@mona$ ./python -m test test_x
170
vstinner@mona$ ./python -m test -j1 test_x
164
I propose to try to make more imports lazy: only import the module the first time that it's needed, tests are rarely "performance sensitive". It may improve the "startup time".
Linked PRs
- gh-111881: Import doctest lazily in libregrtest #111884
- gh-111881: Use lazy import in test.support #111885
- gh-111881: Import lazily linecache in traceback #111886
- gh-111881: Use lazy import in unittest #111887
- gh-111881: Use lazy import in doctest #111888
- gh-111881: Import _sha2 lazily in random #111889
- [3.12] gh-111881: Use lazy import in test.support (#111885) #111890
- [3.12] gh-111881: Import doctest lazily in libregrtest (GH-111884) #111893
- [3.11] gh-111881: Import doctest lazily in libregrtest (GH-111884) #111894
- [3.11] gh-111881: Use lazy import in test.support (#111885) (#111890) #111902
- gh-111881: Import lazily zipfile in support.script_helper #112172