Skip to content

-X lazy_imports=all should not apply to exec() inside functions #149642

Closed

Description

@JelleZijlstra

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

Activity

  1. added
    interpreter-core(Objects, Python, Grammar, and Parser dirs)
    3.16new features, bugs and security fixes
    on May 10, 2026
  2. added 2 commits that reference this issue on May 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.15bugs and security fixes3.16new 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