Title: Importing `bz2` will succeed under `lazy_imports=all` even if bz2 is not supported · Issue #150167 · python/cpython · GitHub
Open Graph Title: Importing `bz2` will succeed under `lazy_imports=all` even if bz2 is not supported · Issue #150167 · python/cpython
X Title: Importing `bz2` will succeed under `lazy_imports=all` even if bz2 is not supported · Issue #150167 · python/cpython
Description: Bug report Edit: Originally this bug was expressed through shutil's support detection. shutil now directly imports from _bz2 which works around this issue. Bug description: Under lazy_imports=all, modules imported in a try/except block a...
Open Graph Description: Bug report Edit: Originally this bug was expressed through shutil's support detection. shutil now directly imports from _bz2 which works around this issue. Bug description: Under lazy_imports=all, ...
X Description: Bug report Edit: Originally this bug was expressed through shutil's support detection. shutil now directly imports from _bz2 which works around this issue. Bug description: Under lazy_imports=a...
Opengraph URL: https://github.com/python/cpython/issues/150167
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Importing `bz2` will succeed under `lazy_imports=all` even if bz2 is not supported","articleBody":"# Bug report\n\nEdit: Originally this bug was expressed through `shutil`'s support detection. `shutil` now directly imports from `_bz2` which works around this issue.\n\n### Bug description:\n\nUnder `lazy_imports=all`, modules imported in a `try/except` block are still imported eagerly. However, imports that occur within that module are still imported lazily.\n\nThis may be intentional, but this bug is the result of that behaviour.\n\nOn CPython built without bz2 support:\n\n```sh\n$ PYTHON_LAZY_IMPORTS=normal ./python -c \"import shutil; print(shutil._BZ2_SUPPORTED)\"\nFalse\n$ PYTHON_LAZY_IMPORTS=all ./python -c \"import shutil; print(shutil._BZ2_SUPPORTED)\"\nTrue\n```\n\nThe `try/except` check is on `bz2`, which succeeds because the import to `_bz2` which would fail is converted to a lazy import and is not used at top level so the import which would fail isn't triggered.\n\nhttps://github.com/python/cpython/blob/87a879f4d0ec2e545e84c898c5ce452a6c87b09e/Lib/shutil.py#L21-L26\n\n`shutil` could directly check `_bz2` in this particular case, but I expect this pattern isn't uncommon. I looked into this after I ran into a similar issue with `pip` and `rich` with an OS-specific import that should have failed in a similar manner.\n\nRelated: https://github.com/python/cpython/issues/149640 as this also causes `test_shutil` to fail under `lazy_imports=all` if `_bz2` isn't present.\n\n### CPython versions tested on:\n\nCPython main branch\n\n### Operating systems tested on:\n\n_No response_\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-150279\n* gh-154771\n* gh-157321\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/DavidCEllis","@type":"Person","name":"DavidCEllis"},"datePublished":"2026-05-20T17:33:15Z","url":"https://github.com/python/cpython/issues/150167"}
| 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:f7d324dd-acf3-8ba4-56e4-33716ab06348 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D978:38EE04:18BC386:20AA76A:6B020E10 |
| html-safe-nonce | 827f2b641f3fcb5142dcd66bf57f29588d541f1f5e84b5f07f3136cd2dde6823 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEOTc4OjM4RUUwNDoxOEJDMzg2OjIwQUE3NkE6NkIwMjBFMTAiLCJ2aXNpdG9yX2lkIjoiNjMwNTM1NDE3MjM2Nzc2OTEwNCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 51be52ec0913402872ab673a725c82aa80ad94967cfefc0f781465be34e03b4c |
| hovercard-subject-tag | issue:4488599120 |
| 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/150167/issue_layout |
| twitter:image | https://opengraph.githubassets.com/d5c69c38d0119832f32674efc2cea4606a7121137c4a9d73f5545e7fd908abb2/python/cpython/issues/150167 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d5c69c38d0119832f32674efc2cea4606a7121137c4a9d73f5545e7fd908abb2/python/cpython/issues/150167 |
| og:image:alt | Bug report Edit: Originally this bug was expressed through shutil's support detection. shutil now directly imports from _bz2 which works around this issue. Bug description: Under lazy_imports=all, ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | DavidCEllis |
| 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