Title: Imports in a `finally` or `else` clause are lazy under `__lazy_modules__` · Issue #162573 · python/cpython · GitHub
Open Graph Title: Imports in a `finally` or `else` clause are lazy under `__lazy_modules__` · Issue #162573 · python/cpython
X Title: Imports in a `finally` or `else` clause are lazy under `__lazy_modules__` · Issue #162573 · python/cpython
Description: Bug report Bug description: The docs say "Imports inside functions, class bodies, or try/except/finally blocks are always eager, regardless of __lazy_modules__" (Compatibility via __lazy_modules__), and lazy import in a finally or else c...
Open Graph Description: Bug report Bug description: The docs say "Imports inside functions, class bodies, or try/except/finally blocks are always eager, regardless of __lazy_modules__" (Compatibility via __lazy_modules__)...
X Description: Bug report Bug description: The docs say "Imports inside functions, class bodies, or try/except/finally blocks are always eager, regardless of __lazy_modules__" (Compatibility via __lazy_...
Opengraph URL: https://github.com/python/cpython/issues/162573
X: @github
Domain: github.com
IPv4 address: 140.82.112.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Imports in a `finally` or `else` clause are lazy under `__lazy_modules__`","articleBody":"# Bug report\n\n### Bug description:\n\nThe docs say \"Imports inside functions, class bodies, or try/except/finally blocks are always eager, regardless of `__lazy_modules__`\" ([Compatibility via `__lazy_modules__`](https://docs.python.org/dev/reference/simple_stmts.html#lazy-modules-compat)), and `lazy import` in a `finally` or `else` clause is a SyntaxError (\"lazy import not allowed inside try/except blocks\"). Imports in the `try` and `except` bodies are eager, but one in the `finally` clause, on the normal path, or in the `else` clause of a `try` without `finally` is lazy:\n\n```python\n__lazy_modules__ = [\"ast\", \"json\"]\ntry:\n pass\nexcept ImportError:\n pass\nelse:\n import ast\ntry:\n raise KeyError\nexcept KeyError:\n pass\nfinally:\n import json\nprint(type(globals()[\"ast\"]).__name__, type(globals()[\"json\"]).__name__)\n```\n\n```\nlazy_import lazy_import\n```\n\nI expected `module module`. The normal-path copy of the `finally` body (`codegen_try_finally` in `Python/codegen.c`) and the `else` body are compiled outside the fblocks that `_PyCompile_InExceptionHandler` checks. I found it while writing a Lean spec of Python.\n\n### CPython versions tested on:\n\n3.15, CPython main branch\n\n### Operating systems tested on:\n\nLinux\n","author":{"url":"https://github.com/Robertboy18","@type":"Person","name":"Robertboy18"},"datePublished":"2026-11-24T17:33:28Z","url":"https://github.com/python/cpython/issues/162573"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:578e3a4e-f4f9-b091-bbad-4c04d8b5b987 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D20E:DA7D9:B960D0:F6647B:6B06420B |
| html-safe-nonce | 16f3a49680cdb03aeb37369ab337726545602e46f6e7513fa1614a15d5b83400 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMjBFOkRBN0Q5OkI5NjBEMDpGNjY0N0I6NkIwNjQyMEIiLCJ2aXNpdG9yX2lkIjoiMjc0NjAyNDU3MTI2OTY5Mzk2MyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 20215e163c5e88b2001bad80a98fbf0b708a3f45778d1f7595c09fa8ab257d3b |
| hovercard-subject-tag | issue:6587703036 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/162573/issue_layout |
| twitter:image | https://opengraph.githubassets.com/cdc3264a685bcfe97a5e4e305506a5ce08df28dafda474295ca5c939ed81eaa9/python/cpython/issues/162573 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/cdc3264a685bcfe97a5e4e305506a5ce08df28dafda474295ca5c939ed81eaa9/python/cpython/issues/162573 |
| og:image:alt | Bug report Bug description: The docs say "Imports inside functions, class bodies, or try/except/finally blocks are always eager, regardless of __lazy_modules__" (Compatibility via __lazy_modules__)... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Robertboy18 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 1bff4f6bfaa96dc9c42cacf315424c24ebb548b06142ced5e3aaccb2a42dd652 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | ac613d3698d3d8565651455086e1f74da07bf277 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width