Title: `__import__` too eager, make it lazy · Issue #161721 · python/cpython · GitHub
Open Graph Title: `__import__` too eager, make it lazy · Issue #161721 · python/cpython
X Title: `__import__` too eager, make it lazy · Issue #161721 · python/cpython
Description: Bug report We have an inconsistency: import
Open Graph Description: Bug report We have an inconsistency: import
X Description: Bug report We have an inconsistency: import <module> -> ModuleType | LazyImportType lazy import <module> -> ModuleType | LazyImportType __lazy_import__(<module>) -> Modul...
Opengraph URL: https://github.com/python/cpython/issues/161721
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`__import__` too eager, make it lazy","articleBody":"### Bug report\n\nWe have an **inconsistency**:\n\n`import \u003cmodule>` -> `ModuleType | LazyImportType`\n`lazy import \u003cmodule>` -> `ModuleType | LazyImportType`\n`__lazy_import__(\u003cmodule>)` -> `ModuleType | LazyImportType`\n\n`importlib.import_module(\u003cmodule>)` -> **`ModuleType`**\n`importlib.__import__(\u003cmodule>)` ->**`ModuleType`**\n`__import__(\u003cmodule>)` -> **`ModuleType`**\n\nAt its core, the issue seems to be that `__lazy_import__` checks lazy filters, but `__import__` doesn't.\n\nConsider:\n\n```python\nimport sys\n\ndef my_filter(importing, imported, fromlist):\n return True\n\nsys.set_lazy_imports_filter(my_filter)\nsys.set_lazy_imports('all')\n\nimport json\n\nprint(type(globals()['json']))\n```\n\nOutputs `\u003cclass 'lazy_import'>` as expected.\n\nBut switching the `import json` to `json = __import__('json')` (or `importlib.import_module` or `importlib.__import__`) outputs `\u003cclass 'module'>`.\n\nNot only is that inconsistent with `import` stmt, it's inconsistent with `lazy import`. Consider:\n\n```python\nimport sys\n\ndef my_filter(importing, imported, fromlist):\n return False\n\nsys.set_lazy_imports_filter(my_filter)\n\nlazy import json\n\nprint(type(globals()['json']))\n```\n\nThe output changes depending on `True` / `False` in the filter, which is intended. It also works if I switch out `lazy import json` for `json = __lazy_import__('json')`.\n\n### CPython versions tested on:\n\n3.15\n\n### Operating systems tested on:\n\nWindows","author":{"url":"https://github.com/JadenV15","@type":"Person","name":"JadenV15"},"datePublished":"2026-11-10T00:17:15Z","url":"https://github.com/python/cpython/issues/161721"}
| 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:40a3735b-dfea-cdf2-44f3-5c2a07070af0 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8044:14EB6E:16CB713:1DFC0F2:6B01FEAC |
| html-safe-nonce | 5702e58d095e66c9e50bf4c3235d5e4a72445cefdd50b72435af523630afaff5 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MDQ0OjE0RUI2RToxNkNCNzEzOjFERkMwRjI6NkIwMUZFQUMiLCJ2aXNpdG9yX2lkIjoiNjQwMTA5NzY4NTM4NzE3ODY4IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | aba53721b3568417a6dae59d3a0d5134f538c09bb8cb3b7c50ba49ce228c973f |
| hovercard-subject-tag | issue:6297411427 |
| 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/161721/issue_layout |
| twitter:image | https://opengraph.githubassets.com/94878b1ac6d9df1593bef98a4a938d7c0d1044f29e520ea192de45ed88c15c1a/python/cpython/issues/161721 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/94878b1ac6d9df1593bef98a4a938d7c0d1044f29e520ea192de45ed88c15c1a/python/cpython/issues/161721 |
| og:image:alt | Bug report We have an inconsistency: import |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | JadenV15 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| 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 | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width