GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — shutil: avoid importing bz2, lzma and compression.zstd just to probe availability · Issue #154904 · python/cpython

shutil: avoid importing bz2, lzma and compression.zstd just to probe availability #154904

New issue
Copy link
New issue
Copy link
Closed
#154908
Closed
shutil: avoid importing bz2, lzma and compression.zstd just to probe availability#154904
#154908
Copy link
Labels
performancePerformance or resource usagePerformance or resource usagestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement

Description

@maxday
maxday
opened on Jul 30, 2026
Issue body actions

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
    _BZ2_SUPPORTED = True
except ImportError:
    _BZ2_SUPPORTED = False

Every 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.

Net import cost (median of 40 fresh interpreters):

net cost
import shutil 12.75 ms
import urllib.request 36.64 ms

This matters most for short-lived processes on constrained hardware: CLI tools and serverless cold starts, where a few ms per process is worth removing.

I have a patch with tests that cuts this by ~2.5 ms (−19.6%) on import shutil, and will open a PR.

Linked PRs

  • gh-154904: Speed up import of shutil by probing compression extensions #154908

Activity

  1. aisk
    added
    performancePerformance or resource usagePerformance or resource usage
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    on Jul 30, 2026
  2. hugovk
    added a parent issue on Jul 30, 2026
    • Improve import time of various stdlib modules #154675
  3. StanFromIreland commented on Jul 30, 2026

    @StanFromIreland
    StanFromIreland
    on Jul 30, 2026
    Member
    More actions

    (JFYI @aisk, perf improvements are features)

    Reacted by An Long
  4. StanFromIreland
    added
    type-featureA feature request or enhancementA feature request or enhancement
    on Jul 30, 2026
  5. maxday commented on Aug 6, 2026

    @maxday
    maxday
    on Aug 6, 2026
    ContributorAuthor
    More actions

    @StanFromIreland / @aisk
    It seems that the PR has been approved but we're missing a review from the core team, do you know who should I ping?
    (Not sure about the SLA, so let me know if there is process/policy) Thanks!

  6. emmatyping
    closed this as completedin #154908on Aug 11, 2026
  7. emmatyping
    added a commit that references this issue on Aug 11, 2026

    gh-154904: Speed up import of shutil by probing compression extension…

    b98e1a5
  8. mbeijen
    added a commit that references this issue on Aug 14, 2026

    pythongh-154904: Speed up import of shutil by probing compression ext…

    1ff806f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    performancePerformance or resource usagePerformance or resource usagestdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.