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 — gh-154675: Use lazy imports in json package by brittanyrey · Pull Request #156801 · python/cpython

Conversation

@brittanyrey

@brittanyrey brittanyrey commented Sep 1, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

summary

Apply lazy imports to json package to lift nested imports and optimize the modules.

perf

before after
python -c "import json.tool" 41.09 ms 21.24 ms (−48%)
python -c "import json" 20.98 ms 20.81 ms (−0.8%, neutral)
python -m json.tool f.json 46.06 ms 45.87 ms (−0.4%, neutral)
  • Issue: Improve import time of various stdlib modules #154675

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

edvilme reacted with heart emoji
All reactions
  • ❤️ 1 reaction
@brittanyrey
pythongh-154675: Use lazy imports in json.tool
db14e0d
`argparse` and `_colorize` are only needed once `main()` runs, so import
them lazily. Also hoist the nested `warnings._deprecated` import in
`json.__getattr__` up to a module-level `lazy from`.

`python -c "import json.tool"` drops from 41.1 ms to 21.2 ms (-48%):
interleaved A/B, median of 9 rounds of min-of-25 runs, same binary with
`Lib/json` swapped via `PYTHONPATH`; an unrelated `import csv` control
moved +0.2%.

Running the CLI is unchanged -- `main()` always builds an `ArgumentParser`
and always calls `can_colorize()`, so both modules still get imported.
@bedevere-app bedevere-app Bot mentioned this pull request Sep 1, 2026
Improve import time of various stdlib modules #154675
Open
@bedevere-app bedevere-app Bot added the awaiting review label Sep 1, 2026
johnslavik
johnslavik approved these changes Sep 1, 2026
View reviewed changes
@bedevere-app bedevere-app Bot added awaiting core review and removed awaiting review labels Sep 1, 2026
eendebakpt
eendebakpt reviewed Sep 2, 2026
View reviewed changes
Comment thread Misc/NEWS.d/next/Library/2026-09-01-14-20-00.gh-issue-154675.Kq7vZ2.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

@eendebakpt

eendebakpt commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

Are there any use cases where the improved import time matters? Users importing json.tool will run main() and that will trigger imports of the lazy packages.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@johnslavik

johnslavik commented Sep 2, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Are there any use cases where the improved import time matters? Users importing json.tool will run main() and that will trigger imports of the lazy packages.

Don't think so. And json.tool has no import-only use cases. Maybe drop this and news entry and leave only the json lazy import?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@picnixz

picnixz commented Sep 2, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

AFAIR, the PEP explicitly said that we do not want to mechanically convert everything to lazy imports. Considering the noise for json itself I wonder about this change. json.tool is likely to be used as a CLI and there is no reason to do "import json.tool" as the public API is empty IIRC

StanFromIreland and brittanyrey reacted with thumbs up emoji
All reactions
  • 👍 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@picnixz picnixz added the pending The issue will be closed if no feedback is provided label Sep 2, 2026
@brittanyrey @eendebakpt
Update Misc/NEWS.d/next/Library/2026-09-01-14-20-00.gh-issue-154675.K…
b227c65
…q7vZ2.rst

Co-authored-by: Pieter Eendebak <[email protected]>
@brittanyrey

brittanyrey commented Sep 2, 2026

Copy link
Copy Markdown
Contributor Author

Solid call out. Dug in and I completely see what you mean...argparse and colorize will immediately be reified, so this is quite a wash.
I'm happy to abandon this.

johnslavik reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@johnslavik

johnslavik commented Sep 2, 2026

Copy link
Copy Markdown
Member

I'll close then 👍🏽

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@johnslavik johnslavik closed this Sep 2, 2026
@brittanyrey
brittanyrey deleted the b-perf-json-lazy-imports branch September 2, 2026 20:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@eendebakpt eendebakpt eendebakpt left review comments
@johnslavik johnslavik johnslavik approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

awaiting core review pending The issue will be closed if no feedback is provided

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@brittanyrey @eendebakpt @johnslavik @picnixz
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.