Inspired by #149321 I decided to see how well the test suite works with PYTHON_LAZY_IMPORTS=all / -X lazy_imports=all, which makes all imports lazy. It's nowhere near as bad as the none option that we're considering removing, but a few parts of the stdlib are broken, and it seems worth considering if we want to fix these.
test_unittest fails because unittest.main becomes a module instead of a function.
test_struct is broken because of a test that does exec("import struct") in a function; lazy imports are disallowed within functions. Similar issues affect test_enum, test_traceback, and test_future.
test_symtable is broken because it fills its _flags list by iterating over globals(), which means it starts containing lazy import marker objects. Similarly test_inspect breaks because it iterates over vars() of a module.
A few tests fail for legitimate reasons; test_compileall tests that an import triggers pyc compilation; test_builtin asserts that __import__ is used when in fact __lazy_import__ gets used.
And to be clear the outcome I'm looking for here is that we start testing lazy_imports=all in some kind of CI (a buildbot?) and fix the current issues, either by skipping the test conditionally or by fixing some aspect of the implementation.
I'm now looking into the exec issue which feels like a bug; the unittest issue may also be a bug but I need to look further.
Activity
JelleZijlstra commented on May 10, 2026
And to be clear the outcome I'm looking for here is that we start testing lazy_imports=all in some kind of CI (a buildbot?) and fix the current issues, either by skipping the test conditionally or by fixing some aspect of the implementation.
I'm now looking into the exec issue which feels like a bug; the unittest issue may also be a bug but I need to look further.
brittanyrey commented on Jun 9, 2026
Attempting to add the CI in the linked PR.
Also adding an exclusion list for failing tests to steadily chip away at
gh-149640: Add new GitHub action to test lazy_imports=all against tes…
StanFromIreland commented on Oct 1, 2026
It's now tested in the CI, so I'm going to close this.