Title: shutil: avoid importing bz2, lzma and compression.zstd just to probe availability · Issue #154904 · python/cpython · GitHub
Open Graph Title: shutil: avoid importing bz2, lzma and compression.zstd just to probe availability · Issue #154904 · python/cpython
X Title: shutil: avoid importing bz2, lzma and compression.zstd just to probe availability · Issue #154904 · python/cpython
Description: shutil imports bz2/lzma/zstd at startup just to check they exist shutil imports bz2, lzma and compression.zstd at module scope purely to discover whether those formats are available, then throws the bindings away: try: import bz2 del bz2...
Open Graph Description: shutil imports bz2/lzma/zstd at startup just to check they exist shutil imports bz2, lzma and compression.zstd at module scope purely to discover whether those formats are available, then throws th...
X Description: shutil imports bz2/lzma/zstd at startup just to check they exist shutil imports bz2, lzma and compression.zstd at module scope purely to discover whether those formats are available, then throws th...
Opengraph URL: https://github.com/python/cpython/issues/154904
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"shutil: avoid importing bz2, lzma and compression.zstd just to probe availability","articleBody":"`shutil` imports bz2/lzma/zstd at startup just to check they exist\n\n`shutil` imports `bz2`, `lzma` and `compression.zstd` at module scope purely to discover whether those formats are available, then throws the bindings away:\n\n```python\ntry:\n import bz2\n del bz2\n _BZ2_SUPPORTED = True\nexcept ImportError:\n _BZ2_SUPPORTED = False\n```\n\nEvery program importing `shutil` pays for that, even if it never creates or extracts an archive and `shutil` is imported transitively by much of the stdlib: on `main`, each of `urllib.request`, `tempfile`, `zipfile`, `tarfile`, `http.server` and `venv` ends up with `shutil` in `sys.modules`.\n\nNet import cost (median of 40 fresh interpreters):\n\n| | net cost |\n|---|---|\n| `import shutil` | 12.75 ms |\n| `import urllib.request` | 36.64 ms |\n\nThis matters most for short-lived processes on constrained hardware: CLI tools and serverless cold starts, where a few ms per process is worth removing.\n\nI have a patch with tests that cuts this by ~2.5 ms (−19.6%) on `import shutil`, and will open a PR.\n\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-154908\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/maxday","@type":"Person","name":"maxday"},"datePublished":"2026-07-30T02:27:59Z","url":"https://github.com/python/cpython/issues/154904"}
| 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:b3e9a2b2-4dcf-3a6d-9aa4-0c04b66dbe7f |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | DD16:2270F4:36F21EB:48D9E8A:6B02078F |
| html-safe-nonce | 5fdc97fd3685c3a2ce2df734f8e73fa570576d027dfcd44817922425ff61e32e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJERDE2OjIyNzBGNDozNkYyMUVCOjQ4RDlFOEE6NkIwMjA3OEYiLCJ2aXNpdG9yX2lkIjoiNjY0MTI4NTEwODE0Mzg4NDE3NiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 8ab894d4cdd896c0617d759a4b3dd92c211129dc90d721d80aa36717de85facb |
| hovercard-subject-tag | issue:5015433560 |
| 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/154904/issue_layout |
| twitter:image | https://opengraph.githubassets.com/eb40106e7448f69d87cf2c48d53c97f54cf293a9b663bf60226fff95d8f6d50c/python/cpython/issues/154904 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/eb40106e7448f69d87cf2c48d53c97f54cf293a9b663bf60226fff95d8f6d50c/python/cpython/issues/154904 |
| og:image:alt | shutil imports bz2/lzma/zstd at startup just to check they exist shutil imports bz2, lzma and compression.zstd at module scope purely to discover whether those formats are available, then throws th... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | maxday |
| 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