JelleZijlstra · GitHub

Bug report

Bug description:

$ PYTHON_LAZY_IMPORTS=all ./python.exe
Python 3.16.0a0 (heads/main:fb0e0b7f898, May 10 2026, 07:19:46) [Clang 15.0.0 (clang-1500.3.9.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> def func(): import os
...
>>> func()
>>> def func(): exec("import os")
...
>>> func()
Traceback (most recent call last):
  File "<python-input-3>", line 1, in <module>
    func()
    ~~~~^^
  File "<python-input-2>", line 1, in func
    def func(): exec("import os")
                ~~~~^^^^^^^^^^^^^
  File "<string>", line 1, in <module>
SyntaxError: 'lazy import' is only allowed at module level
>>> 

Split out from #149640; this affects some tests in the test suite. PR incoming.

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Linked PRs

Read the original on github.com ↗