Title: Lazy submodule imports hide exceptions raised while loading the submodule · Issue #153236 · python/cpython · GitHub
Open Graph Title: Lazy submodule imports hide exceptions raised while loading the submodule · Issue #153236 · python/cpython
X Title: Lazy submodule imports hide exceptions raised while loading the submodule · Issue #153236 · python/cpython
Description: Bug report Bug description: Accessing a pending lazy submodule through the parent module can hide the real exception raised while importing the submodule. Reproducer: mkdir -p lel/lol printf 'print("LEL")\n' > lel/__init__.py printf '1/0...
Open Graph Description: Bug report Bug description: Accessing a pending lazy submodule through the parent module can hide the real exception raised while importing the submodule. Reproducer: mkdir -p lel/lol printf 'print...
X Description: Bug report Bug description: Accessing a pending lazy submodule through the parent module can hide the real exception raised while importing the submodule. Reproducer: mkdir -p lel/lol printf 'p...
Opengraph URL: https://github.com/python/cpython/issues/153236
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Lazy submodule imports hide exceptions raised while loading the submodule","articleBody":"# Bug report\n\n### Bug description:\n\nAccessing a pending lazy submodule through the parent module can hide the real\nexception raised while importing the submodule.\n\nReproducer:\n\n```sh\nmkdir -p lel/lol\nprintf 'print(\"LEL\")\\n' > lel/__init__.py\nprintf '1/0\\n' > lel/lol/__init__.py\n./python -c 'lazy import lel.lol; lel.lol'\n```\n\nActual output:\n\n```py\nTraceback (most recent call last):\n File \"\u003cstring>\", line 1, in \u003cmodule>\n lazy import lel.lol; lel.lol\n ^^^^^^^\nAttributeError: module 'lel' has no attribute 'lol'\nLEL\n```\n\nThe eager import is much easier to debug:\n\n```sh\n./python -c 'import lel.lol; lel.lol'\n```\n\n```py\nTraceback (most recent call last):\n File \"\u003cstring>\", line 1, in \u003cmodule>\n import lel.lol; lel.lol\n ^^^^^^^^^^^^^^\n File \"/tmp/lazy-issue-repro/lel/lol/__init__.py\", line 1, in \u003cmodule>\n 1/0\n ~^~\nZeroDivisionError: division by zero\nLEL\n```\n\nI would expect the lazy import path to propagate the exception raised while\nexecuting `lel.lol`, probably with the lazy-import reification chain, instead\nof turning it into a missing attribute on `lel`.\n\nUnless I am missing something, this is coming from\n`Objects/moduleobject.c:try_load_lazy_submodule()`. It calls\n`_PyImport_TryLoadLazySubmodule()`, and if that returns `NULL`, it clears the\nactive exception:\n\n```c\nPyObject *result = _PyImport_TryLoadLazySubmodule(mod_name, name);\nPy_DECREF(mod_name);\nif (result == NULL) {\n PyErr_Clear();\n return NULL;\n}\n```\n\nBut `_PyImport_TryLoadLazySubmodule()` calls `PyImport_ImportModuleLevelObject()`,\nso `NULL` can mean that the pending submodule exists but failed while importing,\nnot only that the attribute should fall through to normal `AttributeError`\nhandling.\n\nThis seems related to gh-150052 / gh-150744, but it looks different from\ngh-151208: there is no parent attribute shadowing here, the child module itself\nraises during import.\n\n### CPython versions tested on:\n\nCPython main branch\n\nTested on a local `3.16.0a0` main-derived build at `8b59df84c0d`, containing\n`5a2d2736a655d993934adee1bbb568067d53f6f4`.\n\n### Operating systems tested on:\n\nLinux\n\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-153237\n* gh-153936\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/pablogsal","@type":"Person","name":"pablogsal"},"datePublished":"2026-07-06T20:01:48Z","url":"https://github.com/python/cpython/issues/153236"}
| 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:7e4e57fe-9b6a-b740-a03b-392c804a4ad9 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8F62:3DD488:378F9AA:46A639C:6B01F34D |
| html-safe-nonce | 3a8fed971c4573ed96995fb6b9f4f8f94e73800245dde2d404f3ae32ebef8c71 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4RjYyOjNERDQ4ODozNzhGOUFBOjQ2QTYzOUM6NkIwMUYzNEQiLCJ2aXNpdG9yX2lkIjoiMjg4NzU5MzgwNTgxMTQxMzgzOCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 60660ef5471407f0f7d03a4d5067cf7b8651c227c422f9c436e6a4cda6490b7f |
| hovercard-subject-tag | issue:4822663789 |
| 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/153236/issue_layout |
| twitter:image | https://opengraph.githubassets.com/6464bd783452cd88c159530f955c9b6c13a7d4b193be3054fd27fbf2f077fc3d/python/cpython/issues/153236 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/6464bd783452cd88c159530f955c9b6c13a7d4b193be3054fd27fbf2f077fc3d/python/cpython/issues/153236 |
| og:image:alt | Bug report Bug description: Accessing a pending lazy submodule through the parent module can hide the real exception raised while importing the submodule. Reproducer: mkdir -p lel/lol printf 'print... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | pablogsal |
| 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