Title: `lazy import a.b as c` resolves `b` as an attribute of `a` instead of importing the module `a.b` · Issue #157757 · python/cpython · GitHub
Open Graph Title: `lazy import a.b as c` resolves `b` as an attribute of `a` instead of importing the module `a.b` · Issue #157757 · python/cpython
X Title: `lazy import a.b as c` resolves `b` as an attribute of `a` instead of importing the module `a.b` · Issue #157757 · python/cpython
Description: Bug report Bug description: lazy import a.b as c never imports the module a.b by name. On first use it imports a and reads the attribute b off it, as lazy from a import b would, so c becomes whatever that attribute lookup finds. This man...
Open Graph Description: Bug report Bug description: lazy import a.b as c never imports the module a.b by name. On first use it imports a and reads the attribute b off it, as lazy from a import b would, so c becomes whatev...
X Description: Bug report Bug description: lazy import a.b as c never imports the module a.b by name. On first use it imports a and reads the attribute b off it, as lazy from a import b would, so c becomes whatev...
Opengraph URL: https://github.com/python/cpython/issues/157757
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`lazy import a.b as c` resolves `b` as an attribute of `a` instead of importing the module `a.b`","articleBody":"# Bug report\n\n### Bug description:\n\n`lazy import a.b as c` never imports the module `a.b` by name. On first use it imports `a` and reads the attribute `b` off it, as `lazy from a import b` would, so `c` becomes whatever that attribute lookup finds. This manifests in two ways.\n\n**Case A**: `a.b` is not a module. The eager statement raises; the lazy one binds the attribute.\n\n```python\nlazy import math.pi as lazy_x\nprint(lazy_x) # 3.141592653589793\n\nimport math.pi as x # ModuleNotFoundError: No module named 'math.pi'; 'math' is not a package\n```\n\nThe bare `lazy import math.pi` was fixed in #155194; the `as` form was not.\n\n**Case B**: `a.b` is a module, but `a` has an attribute `b` that shadows it and `a.b` has not been imported yet. The eager statement imports the submodule, which rebinds `a.b` to the module; the lazy one binds the attribute and never imports the submodule. `json` does not import `json.tool`, so the assignment below stands in for an attribute a package's `__init__.py` would define:\n\n```python\nimport json\njson.tool = \"attribute\"\n\nlazy import json.tool as lazy_c\nprint(type(lazy_c).__name__) # str\n\nimport json.tool as c\nprint(type(c).__name__) # module\n```\n\nCase B is related to #151208, which reports the same shadowing for `import a.foo` under `-X lazy_imports=all` followed by an attribute access `a.foo`, resolved in `Objects/moduleobject.c`. That script now passes on `main`; this statement still fails, because it is resolved on a different path.\n\n### Possible root cause\n\n`import a.b as c` compiles to `IMPORT_NAME a.b` followed by `IMPORT_FROM b`. Lazily, `IMPORT_NAME` leaves a placeholder holding `a.b`, and `_PyEval_LazyImportFrom` ([Python/ceval.c:3329-3384](https://github.com/python/cpython/blob/0a6c1ed34118b091230ee38fc047bcc2df8e5c5e/Python/ceval.c#L3329-L3384)) replaces it with one holding `lz_from = \"a\"`, `lz_attr = \"b\"`. That is the same placeholder `lazy from a import b` produces.\n\nReification in `_PyImport_LoadLazyImportTstate` ([Python/import.c:3902-4093](https://github.com/python/cpython/blob/0a6c1ed34118b091230ee38fc047bcc2df8e5c5e/Python/import.c#L3902-L4093)) therefore runs `__import__(\"a\", fromlist=(\"b\",))` followed by `getattr(a, \"b\")` for both statements. Nothing records that the statement was an `import`, so the module `a.b` is never imported by its own name: the \"is not a package\" check never runs, and an existing attribute `b` is never overwritten by the submodule.\n\nThis is not #157614, which is about the `sys.modules` fast path in `_PyEval_LazyImportFrom` consulting the wrong module. The present issue reproduces with the fix in #157626 applied.\n\n\n### CPython versions tested on:\n\n3.15\n\n### Operating systems tested on:\n\nLinux\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-158092\n* gh-158257\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/grgalex","@type":"Person","name":"grgalex"},"datePublished":"2026-09-18T14:51:53Z","url":"https://github.com/python/cpython/issues/157757"}
| 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:0f44834d-dcde-452a-8bd9-fb69932cf1c7 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C294:1D86C4:39C415E:496FC05:6B01F9EF |
| html-safe-nonce | c822d8e34ad6284a983f1c89b997fb3b66606130b1ca6f5b46c7cc7590e2d6df |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMjk0OjFEODZDNDozOUM0MTVFOjQ5NkZDMDU6NkIwMUY5RUYiLCJ2aXNpdG9yX2lkIjoiNzQxNDUxNzE0MTQxMzY4OTg0MCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 8be3df9239e157d0c06177bc471861fdabed69db393a8c6a65b3e7382273c9f1 |
| hovercard-subject-tag | issue:5500997289 |
| 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/157757/issue_layout |
| twitter:image | https://opengraph.githubassets.com/a3a72662630f1d33c75e7cc9f34adc5367059bb740f6bcfc9cac4fc603a8badb/python/cpython/issues/157757 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/a3a72662630f1d33c75e7cc9f34adc5367059bb740f6bcfc9cac4fc603a8badb/python/cpython/issues/157757 |
| og:image:alt | Bug report Bug description: lazy import a.b as c never imports the module a.b by name. On first use it imports a and reads the attribute b off it, as lazy from a import b would, so c becomes whatev... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | grgalex |
| 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