Skip to content

What is supposed to work under -X lazy_imports=none? #149321

@ZeroIntensity

Description

@ZeroIntensity

After seeing #149318, I was curious as to what the effect would be when run with -X lazy_imports=none. To my surprise, I noticed that very little seems to work under the setting already.

For example, trying to run the test suite fails:

$ ./python -Xlazy_imports=none -m test
Exception ignored in the internal traceback machinery:
Traceback (most recent call last):
  File "/home/peter/develop/cpython/Lib/traceback.py", line 15, in <module>
    import tokenize
  File "/home/peter/develop/cpython/Lib/tokenize.py", line 38, in <module>
    lazy import _colorize
  File "/home/peter/develop/cpython/Lib/_colorize.py", line 6, in <module>
    from dataclasses import dataclass, field, Field
  File "/home/peter/develop/cpython/Lib/dataclasses.py", line 5, in <module>
    import annotationlib
  File "/home/peter/develop/cpython/Lib/annotationlib.py", line 3, in <module>
    import ast
  File "/home/peter/develop/cpython/Lib/ast.py", line 24, in <module>
    lazy from _colorize import can_colorize, get_theme
ImportError: cannot import name 'can_colorize' from partially initialized module '_colorize' (most likely due to a circular import) (/home/peter/develop/cpython/Lib/_colorize.py)
Traceback (most recent call last):
  File "/home/peter/develop/cpython/Lib/runpy.py", line 201, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/home/peter/develop/cpython/Lib/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/home/peter/develop/cpython/Lib/test/__main__.py", line 1, in <module>
    from test.libregrtest.main import main
  File "/home/peter/develop/cpython/Lib/test/libregrtest/main.py", line 8, in <module>
    import trace
  File "/home/peter/develop/cpython/Lib/trace.py", line 56, in <module>
    import tokenize
  File "/home/peter/develop/cpython/Lib/tokenize.py", line 38, in <module>
    lazy import _colorize
  File "/home/peter/develop/cpython/Lib/_colorize.py", line 6, in <module>
    from dataclasses import dataclass, field, Field
  File "/home/peter/develop/cpython/Lib/dataclasses.py", line 5, in <module>
    import annotationlib
  File "/home/peter/develop/cpython/Lib/annotationlib.py", line 3, in <module>
    import ast
  File "/home/peter/develop/cpython/Lib/ast.py", line 24, in <module>
    lazy from _colorize import can_colorize, get_theme
ImportError: cannot import name 'can_colorize' from partially initialized module '_colorize' (most likely due to a circular import) (/home/peter/develop/cpython/Lib/_colorize.py)

The new REPL doesn't work:

$ ./python -X lazy_imports=none
Python 3.15.0a8+ (heads/pr_149318:6a88c2deeb3, May  3 2026, 10:35:20) [GCC 15.2.1 20260209] on linux
Type "help", "copyright", "credits" or "license" for more information.
Failed calling sys.__interactivehook__
Exception ignored in the internal traceback machinery:
Traceback (most recent call last):
  File "/home/peter/develop/cpython/Lib/traceback.py", line 15, in <module>
    import tokenize
  File "/home/peter/develop/cpython/Lib/tokenize.py", line 38, in <module>
    lazy import _colorize
  File "/home/peter/develop/cpython/Lib/_colorize.py", line 170, in <module>
    @dataclass(frozen=True, kw_only=True)
  File "/home/peter/develop/cpython/Lib/dataclasses.py", line 1456, in wrap
    return _process_class(cls, init, repr, eq, order, unsafe_hash,
  File "/home/peter/develop/cpython/Lib/dataclasses.py", line 1064, in _process_class
    cls_annotations = annotationlib.get_annotations(
AttributeError: module 'annotationlib' has no attribute 'get_annotations'
Traceback (most recent call last):
  File "/home/peter/develop/cpython/Lib/site.py", line 513, in register_readline
    import rlcompleter  # noqa: F401
  File "/home/peter/develop/cpython/Lib/rlcompleter.py", line 34, in <module>
    import inspect
  File "/home/peter/develop/cpython/Lib/inspect.py", line 146, in <module>
    from annotationlib import Format, ForwardRef
  File "/home/peter/develop/cpython/Lib/annotationlib.py", line 3, in <module>
    import ast
  File "/home/peter/develop/cpython/Lib/ast.py", line 24, in <module>
    lazy from _colorize import can_colorize, get_theme
  File "/home/peter/develop/cpython/Lib/_colorize.py", line 170, in <module>
    @dataclass(frozen=True, kw_only=True)
  File "/home/peter/develop/cpython/Lib/dataclasses.py", line 1456, in wrap
    return _process_class(cls, init, repr, eq, order, unsafe_hash,
  File "/home/peter/develop/cpython/Lib/dataclasses.py", line 1064, in _process_class
    cls_annotations = annotationlib.get_annotations(
AttributeError: module 'annotationlib' has no attribute 'get_annotations'
warning: can't use pyrepl: module 'annotationlib' has no attribute 'get_annotations'
>>> 

I compiled a list of all the standard library modules that currently don't work under -X lazy_imports=none:

  1. mailbox
  2. logging
  3. bdb
  4. pdb
  5. ast
  6. tokenize
  7. turtle
  8. ensurepip
  9. inspect
  10. unittest
  11. py_compile
  12. tabnanny
  13. code
  14. asyncio
  15. traceback
  16. antigravity
  17. contextlib
  18. doctest
  19. pathlib
  20. pstats
  21. compileall
  22. zipfile
  23. getpass
  24. zipapp
  25. pydoc
  26. webbrowser
  27. configparser
  28. subprocess
  29. venv
  30. rlcompleter
  31. pyclbr
  32. difflib
  33. trace
  34. annotationlib
  35. imaplib
  36. pickletools
  37. glob

I've already raised my concerns about this on DPO (in fact, LWN wrote an entire article about this), but anyway, what do we do? Do we consider this a bug? If so, how do we test for it? If not, then what is supposed to work? We should document the restrictions and expected behavior for disabling lazy imports.

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.15new features, bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)topic-lazy-importstype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions