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 — argparse in 3.14+ is importing `_colorize` earlier than necessary · Issue #148823 · python/cpython

argparse in 3.14+ is importing _colorize earlier than necessary #148823

New issue
Copy link
New issue
Copy link
Closed
Closed
argparse in 3.14+ is importing _colorize earlier than necessary#148823
Copy link
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@DavidCEllis
DavidCEllis
opened on Apr 21, 2026
Issue body actions

Bug report

Bug description:

I say "earlier than necessary" because argparse does have an in-line lazy import of _colorize so it does seem like this was considered.

However this import is only actually deferred until an ArgumentParser is created:

$ python -c "import sys; import argparse; print(sys.modules.get('_colorize')); argparse.ArgumentParser(); print(sys.modules.get('_colorize'))"
None
<module '_colorize' from '/usr/lib64/python3.14/_colorize.py'>

This results in a noticeable %-wise increase in the effective import time when you actually use the module. Note that some of this time is also a lazy import of shutil which isn't very lazy either, but that's a separate issue.

Benchmark 1: .venv_313/bin/python -c 'import argparse; argparse.ArgumentParser()'
  Time (mean ± σ):      15.6 ms ±   3.0 ms    [User: 13.6 ms, System: 2.1 ms]
  Range (min … max):    11.3 ms …  27.9 ms    200 runs
 
Benchmark 2: .venv_314/bin/python -c 'import argparse; argparse.ArgumentParser()'
  Time (mean ± σ):      25.2 ms ±   3.6 ms    [User: 21.9 ms, System: 3.2 ms]
  Range (min … max):    19.9 ms …  39.5 ms    200 runs
 
Benchmark 3: .venv_315/bin/python -c 'import argparse; argparse.ArgumentParser()'
  Time (mean ± σ):      25.7 ms ±   3.2 ms    [User: 22.6 ms, System: 3.1 ms]
  Range (min … max):    21.0 ms …  34.7 ms    200 runs
 
Summary
  .venv_313/bin/python -c 'import argparse; argparse.ArgumentParser()' ran
    1.61 ± 0.39 times faster than .venv_314/bin/python -c 'import argparse; argparse.ArgumentParser()'
    1.65 ± 0.38 times faster than .venv_315/bin/python -c 'import argparse; argparse.ArgumentParser()'

This gets a bit more noticeable in real terms on slower devices, so it would be nice to defer this until something actually needs colouring. Note that there are some plans to make _colorize faster, but I'm not sure how much faster it will be as an end result so this may still be worth doing separately to that work.

CPython versions tested on:

3.14, 3.15

Operating systems tested on:

Linux

Linked PRs

  • gh-148823: Avoid importing _colorize when creating an ArgumentParser #148827

Activity

  1. DavidCEllis
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Apr 21, 2026
  2. hugovk
    added this to Argparse issueson Apr 21, 2026
  3. DavidCEllis commented on Apr 21, 2026

    @DavidCEllis
    DavidCEllis
    on Apr 21, 2026
    ContributorAuthor
    More actions

    I meant to note that the shutil import is already present in 3.13, hence being a separate issue - but it has gotten slightly slower due to the addition of compression.zstd which it now imports.

  4. picnixz
    added
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    on Apr 21, 2026
  5. savannahostrowski
    added a commit that references this issue on May 6, 2026

    gh-148823: Avoid importing `_colorize` when creating an `ArgumentPars…

    8cad740
  6. DavidCEllis commented on May 6, 2026

    @DavidCEllis
    DavidCEllis
    on May 6, 2026
    ContributorAuthor
    More actions

    Closed by #148827

    Reacted by Savannah Ostrowski
  7. DavidCEllis
    closed this as completedon May 6, 2026
  8. github-project-automation
    moved this to Doc issues in Argparse issueson May 6, 2026
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

    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    • Argparse issues
      Status
      Doc issues
      Show more project fields

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402520 — 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.