René's URL Explorer Experiment


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

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@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-profiling0
react-import-mapreact,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-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:0f44834d-dcde-452a-8bd9-fb69932cf1c7
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idC294:1D86C4:39C415E:496FC05:6B01F9EF
html-safe-noncec822d8e34ad6284a983f1c89b997fb3b66606130b1ca6f5b46c7cc7590e2d6df
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMjk0OjFEODZDNDozOUM0MTVFOjQ5NkZDMDU6NkIwMUY5RUYiLCJ2aXNpdG9yX2lkIjoiNzQxNDUxNzE0MTQxMzY4OTg0MCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9
visitor-hmac8be3df9239e157d0c06177bc471861fdabed69db393a8c6a65b3e7382273c9f1
hovercard-subject-tagissue:5500997289
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/157757/issue_layout
twitter:imagehttps://opengraph.githubassets.com/a3a72662630f1d33c75e7cc9f34adc5367059bb740f6bcfc9cac4fc603a8badb/python/cpython/issues/157757
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/a3a72662630f1d33c75e7cc9f34adc5367059bb740f6bcfc9cac4fc603a8badb/python/cpython/issues/157757
og:image:altBug 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:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamegrgalex
hostnamegithub.com
expected-hostnamegithub.com
None87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456
turbo-cache-controlno-preview
go-importgithub.com/python/cpython git https://github.com/python/cpython.git
octolytics-dimension-user_id1525981
octolytics-dimension-user_loginpython
octolytics-dimension-repository_id81598961
octolytics-dimension-repository_nwopython/cpython
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id81598961
octolytics-dimension-repository_network_root_nwopython/cpython
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release423dbb54c5e7bfecc838eb387c1435eb3d0e760d
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/python/cpython/issues/157757#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F157757
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fcpython%2Fissues%2F157757
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=python%2Fcpython
Reloadhttps://github.com/python/cpython/issues/157757
Reloadhttps://github.com/python/cpython/issues/157757
Reloadhttps://github.com/python/cpython/issues/157757
pythonhttps://github.com/python
cpythonhttps://github.com/python/cpython
Codehttps://github.com/python/cpython
Issues5k+ (5k+)https://github.com/python/cpython/issues
Pull requests2.4k (2.4k)https://github.com/python/cpython/pulls
Actionshttps://github.com/python/cpython/actions
Projectshttps://github.com/python/cpython/projects
Security and qualityhttps://github.com/python/cpython/security
Insightshttps://github.com/python/cpython/insights
https://github.com/python/cpython
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/157757
New issuehttps://github.com/login?return_to=https://github.com/python/cpython/issues/157757
lazy import a.b as c resolves b as an attribute of a instead of importing the module a.bhttps://github.com/python/cpython/issues/157757#top
3.15bugs and security fixeshttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%223.15%22
3.16new features, bugs and security fixeshttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%223.16%22
interpreter-core(Objects, Python, Grammar, and Parser dirs)https://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22interpreter-core%22
release-blockerhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22release-blocker%22
topic-lazy-importshttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-lazy-imports%22
type-bugAn unexpected behavior, bug, or errorhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-bug%22
https://github.com/grgalex
grgalexhttps://github.com/grgalex
on Sep 18, 2026https://github.com/python/cpython/issues/157757#issue-5500997289
#155194https://github.com/python/cpython/issues/155194
#151208https://github.com/python/cpython/issues/151208
Python/ceval.c:3329-3384https://github.com/python/cpython/blob/0a6c1ed34118b091230ee38fc047bcc2df8e5c5e/Python/ceval.c#L3329-L3384
Python/import.c:3902-4093https://github.com/python/cpython/blob/0a6c1ed34118b091230ee38fc047bcc2df8e5c5e/Python/import.c#L3902-L4093
#157614https://github.com/python/cpython/issues/157614
#157626https://github.com/python/cpython/pull/157626
gh-157757: Fix to make lazy import a.b as c import the module a.b #158092https://github.com/python/cpython/pull/158092
[3.15] gh-157757: Fix to make lazy import a.b as c import the module a.b (GH-158092) #158257https://github.com/python/cpython/pull/158257
grgalexhttps://github.com/grgalex
type-bugAn unexpected behavior, bug, or errorhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-bug%22
on Sep 18, 2026https://github.com/python/cpython/issues/157757#event-31397425574
StanFromIrelandhttps://github.com/StanFromIreland
interpreter-core(Objects, Python, Grammar, and Parser dirs)https://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22interpreter-core%22
release-blockerhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22release-blocker%22
topic-lazy-importshttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-lazy-imports%22
on Sep 18, 2026https://github.com/python/cpython/issues/157757#event-31402301797
github-project-automationhttps://github.com/apps/github-project-automation
Release and Deferred blockers 🚫https://github.com/orgs/python/projects/2
on Sep 18, 2026https://github.com/python/cpython/issues/157757#event-31402322163
github-project-automationhttps://github.com/apps/github-project-automation
Release and Deferred blockers 🚫https://github.com/orgs/python/projects/2
on Sep 18, 2026https://github.com/python/cpython/issues/157757
https://github.com/johnslavik
johnslavikhttps://github.com/johnslavik
on Sep 18, 2026https://github.com/python/cpython/issues/157757#issuecomment-5732960333
@brittanyreyhttps://github.com/brittanyrey
brittanyreyhttps://github.com/brittanyrey
on Sep 18, 2026https://github.com/python/cpython/issues/157757#event-31419768552
Import the module a lazy `import a.b as c` nameshttps://github.com/brittanyrey/cpython/commit/ad1242d6be185e969a7cf8594f6d4748374bb58f
https://docs.github.com/articles/closing-issues-via-commit-messages
ad1242dhttps://github.com/brittanyrey/cpython/commit/ad1242d6be185e969a7cf8594f6d4748374bb58f
hugovkhttps://github.com/hugovk
3.15bugs and security fixeshttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%223.15%22
3.16new features, bugs and security fixeshttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%223.16%22
on Sep 23, 2026https://github.com/python/cpython/issues/157757#event-31660064012
https://github.com/encukou
encukouhttps://github.com/encukou
on Sep 24, 2026https://github.com/python/cpython/issues/157757#issuecomment-5815290807
@brittanyreyhttps://github.com/brittanyrey
https://github.com/brittanyrey
brittanyreyhttps://github.com/brittanyrey
on Sep 24, 2026https://github.com/python/cpython/issues/157757#issuecomment-5816260511
@encukouhttps://github.com/encukou
#158092https://github.com/python/cpython/pull/158092
https://github.com/johnslavik
johnslavikhttps://github.com/johnslavik
on Sep 24, 2026https://github.com/python/cpython/issues/157757#issuecomment-5816582266
https://github.com/brittanyrey
brittanyreyhttps://github.com/brittanyrey
on Sep 24, 2026https://github.com/python/cpython/issues/157757#issuecomment-5817209763
@encukouhttps://github.com/encukou
@johnslavikhttps://github.com/johnslavik
pablogsalhttps://github.com/pablogsal
on Sep 26, 2026https://github.com/python/cpython/issues/157757#event-31904028935
pythongh-157757: Preserve empty fromlists for custom import hookshttps://github.com/brittanyrey/cpython/commit/48244f18b42c9dc180153e2e3e28aeefd0e6a3d4
pythongh-157757https://github.com/python/cpython/issues/157757
48244f1https://github.com/brittanyrey/cpython/commit/48244f18b42c9dc180153e2e3e28aeefd0e6a3d4
gh-157757: Fix to make `lazy import a.b as c` import the module `a.b` (…https://github.com/python/cpython/commit/1e8ff18a1215b711136b4377282e392583847d62
gh-157757https://github.com/python/cpython/issues/157757
…https://github.com/python/cpython/pull/158092
1e8ff18https://github.com/python/cpython/commit/1e8ff18a1215b711136b4377282e392583847d62
[3.15] gh-157757: Fix to make `lazy import a.b as c` import the modul…https://github.com/python/cpython/commit/1a2d24e3bf410b728c4f592bde0be40d3e024dea
gh-157757https://github.com/python/cpython/issues/157757
1a2d24ehttps://github.com/python/cpython/commit/1a2d24e3bf410b728c4f592bde0be40d3e024dea
hugovkhttps://github.com/hugovk
completedhttps://github.com/python/cpython/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Acompleted
on Sep 26, 2026https://github.com/python/cpython/issues/157757#event-31906903881
Kronuzhttps://github.com/Kronuz
on Sep 28, 2026https://github.com/python/cpython/issues/157757#event-31963593626
Parity check to 3.15 tip 5985fcb: pull pythongh-157757 into lazy importshttps://github.com/Kronuz/cpython/commit/1b4669249f0f1af10e6163f8e4a326ed51570afe
5985fcbhttps://github.com/Kronuz/cpython/commit/5985fcbb43d19dd5caeadec0e2f241a3105a822e
pythongh-157757https://github.com/python/cpython/issues/157757
1b46692https://github.com/Kronuz/cpython/commit/1b4669249f0f1af10e6163f8e4a326ed51570afe
Parity check to 3.15 tip 021f634 (release day): lazy core refactoredhttps://github.com/Kronuz/cpython/commit/b60a5ad5b2c30d28f75496f62601c3e32dcdac75
021f634https://github.com/Kronuz/cpython/commit/021f634ed878173e16e50c009816bb0ff35ae1c6
b60a5adhttps://github.com/Kronuz/cpython/commit/b60a5ad5b2c30d28f75496f62601c3e32dcdac75
[3.15] pythongh-157757: Fix to make `lazy import a.b as c` import the…https://github.com/Kronuz/cpython/commit/0bf1009076f0e438658ab84bc8cd0c30da4255a6
pythongh-157757https://github.com/python/cpython/issues/157757
0bf1009https://github.com/Kronuz/cpython/commit/0bf1009076f0e438658ab84bc8cd0c30da4255a6
Record the pythongh-157757 + pythonGH-158282-behaviors backport (clea…https://github.com/Kronuz/cpython/commit/418fded78244c7faf47d6b3e908f7661a370331f
pythongh-157757https://github.com/python/cpython/issues/157757
pythonGH-158282https://github.com/python/cpython/pull/158282
418fdedhttps://github.com/Kronuz/cpython/commit/418fded78244c7faf47d6b3e908f7661a370331f
Guard chained lazy-import resolver against non-tuple lz_attrhttps://github.com/Kronuz/cpython/commit/2ea30ab46cca9e756a5212eb9cddf5dfacf893bc
2ea30abhttps://github.com/Kronuz/cpython/commit/2ea30ab46cca9e756a5212eb9cddf5dfacf893bc
[3.15] pythongh-157757: Fix to make `lazy import a.b as c` import the…https://github.com/Kronuz/cpython/commit/2279ffbd9e93fa40b06ca4bfe706caea827f4ffe
pythongh-157757https://github.com/python/cpython/issues/157757
2279ffbhttps://github.com/Kronuz/cpython/commit/2279ffbd9e93fa40b06ca4bfe706caea827f4ffe
Guard chained lazy-import resolver against non-tuple lz_attrhttps://github.com/Kronuz/cpython/commit/b68a11b1e052b8b64294de644a08842683414f8a
b68a11bhttps://github.com/Kronuz/cpython/commit/b68a11b1e052b8b64294de644a08842683414f8a
Sign up for freehttps://github.com/signup?return_to=https://github.com/python/cpython/issues/157757
Sign in to commenthttps://github.com/login?return_to=https://github.com/python/cpython/issues/157757
3.15bugs and security fixeshttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%223.15%22
3.16new features, bugs and security fixeshttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%223.16%22
interpreter-core(Objects, Python, Grammar, and Parser dirs)https://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22interpreter-core%22
release-blockerhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22release-blocker%22
topic-lazy-importshttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22topic-lazy-imports%22
type-bugAn unexpected behavior, bug, or errorhttps://github.com/python/cpython/issues?q=state%3Aopen%20label%3A%22type-bug%22
Release and Deferred blockers 🚫https://github.com/orgs/python/projects/2
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.