Title: argparse in 3.14+ is importing `_colorize` earlier than necessary · Issue #148823 · python/cpython · GitHub
Open Graph Title: argparse in 3.14+ is importing `_colorize` earlier than necessary · Issue #148823 · python/cpython
X Title: argparse in 3.14+ is importing `_colorize` earlier than necessary · Issue #148823 · python/cpython
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 ...
Open Graph 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 actua...
X 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 ...
Opengraph URL: https://github.com/python/cpython/issues/148823
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"argparse in 3.14+ is importing `_colorize` earlier than necessary","articleBody":"# Bug report\n\n### Bug description:\n\nI say \"earlier than necessary\" because `argparse` does have an in-line lazy import of `_colorize` so it does seem like this was considered.\n\nHowever this import is only actually deferred until an `ArgumentParser` is created:\n\n```bash\n$ python -c \"import sys; import argparse; print(sys.modules.get('_colorize')); argparse.ArgumentParser(); print(sys.modules.get('_colorize'))\"\n```\n\n```python\nNone\n\u003cmodule '_colorize' from '/usr/lib64/python3.14/_colorize.py'>\n```\n\nThis 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.\n\n```\nBenchmark 1: .venv_313/bin/python -c 'import argparse; argparse.ArgumentParser()'\n Time (mean ± σ): 15.6 ms ± 3.0 ms [User: 13.6 ms, System: 2.1 ms]\n Range (min … max): 11.3 ms … 27.9 ms 200 runs\n \nBenchmark 2: .venv_314/bin/python -c 'import argparse; argparse.ArgumentParser()'\n Time (mean ± σ): 25.2 ms ± 3.6 ms [User: 21.9 ms, System: 3.2 ms]\n Range (min … max): 19.9 ms … 39.5 ms 200 runs\n \nBenchmark 3: .venv_315/bin/python -c 'import argparse; argparse.ArgumentParser()'\n Time (mean ± σ): 25.7 ms ± 3.2 ms [User: 22.6 ms, System: 3.1 ms]\n Range (min … max): 21.0 ms … 34.7 ms 200 runs\n \nSummary\n .venv_313/bin/python -c 'import argparse; argparse.ArgumentParser()' ran\n 1.61 ± 0.39 times faster than .venv_314/bin/python -c 'import argparse; argparse.ArgumentParser()'\n 1.65 ± 0.38 times faster than .venv_315/bin/python -c 'import argparse; argparse.ArgumentParser()'\n```\n\nThis 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](https://github.com/python/cpython/issues/144384), 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.\n\n### CPython versions tested on:\n\n3.14, 3.15\n\n### Operating systems tested on:\n\nLinux\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-148827\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/DavidCEllis","@type":"Person","name":"DavidCEllis"},"datePublished":"2026-04-21T12:22:12Z","url":"https://github.com/python/cpython/issues/148823"}
| 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:5e533e1e-b3df-a883-5c44-97ac57d256dd |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8AE2:EA09B:36C6BA2:4898BD7:6B020797 |
| html-safe-nonce | 118de94802f06c84a8d840cd582768b2e6610908a620ff204eb692c6137072f0 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4QUUyOkVBMDlCOjM2QzZCQTI6NDg5OEJENzo2QjAyMDc5NyIsInZpc2l0b3JfaWQiOiI4MTI0MzY3MjA4NDI1MDY0MzQzIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | f8292bb6df2d81720710644a8962e4046e14383011d5732cbf4773156e432761 |
| hovercard-subject-tag | issue:4302541647 |
| 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/148823/issue_layout |
| twitter:image | https://opengraph.githubassets.com/693af59af15fd754fe1d638bef5ce6a8f9e0fed3b66f7e9a9c3a21c345e3bc6b/python/cpython/issues/148823 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/693af59af15fd754fe1d638bef5ce6a8f9e0fed3b66f7e9a9c3a21c345e3bc6b/python/cpython/issues/148823 |
| og:image:alt | 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 actua... |
| 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