Repository navigation
Description
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
Metadata
Metadata
Assignees
Labels
Projects
- StatusShow more project fieldsDoc issues
Activity
DavidCEllis commentedon Apr 21, 2026
I meant to note that the
shutilimport is already present in 3.13, hence being a separate issue - but it has gotten slightly slower due to the addition ofcompression.zstdwhich it now imports.gh-148823: Avoid importing `_colorize` when creating an `ArgumentPars…
DavidCEllis commentedon May 6, 2026
Closed by #148827