Skip to content

gh-109653: Improve import preformance of tkinter library with lazy imports#148409

Draft
sharktide wants to merge 8 commits intopython:mainfrom
sharktide:patch-2
Draft

gh-109653: Improve import preformance of tkinter library with lazy imports#148409
sharktide wants to merge 8 commits intopython:mainfrom
sharktide:patch-2

Conversation

@sharktide
Copy link
Copy Markdown
Contributor

@sharktide sharktide commented Apr 11, 2026

import enum
import sys
import types
lazy import collections
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

collections and enum modules will always be imported since they are used at module level, so it's pointless and misleading to make them lazy here.

You can verify which modules get imported by running

python -Ximporttime -c 'import tkinter'

You should also provide a benchmark, you can use the output from this command and visualize it with the tuna package.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On it. I would have done the benchmark, but I don’t have access to a computer for the next ~24 hours (currently phone coding). (That’s why I labeled this PR as draft :))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, sorry, I noticed the Draft state only after I wrote the comment :-)

TkVersion = float(_tkinter.TK_VERSION)
TclVersion = float(_tkinter.TCL_VERSION)

lazy import re
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move to the top with the other imports

Copy link
Copy Markdown
Contributor Author

@sharktide sharktide Apr 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I still need to get re out of __all__ however. Once everything is finished, I’ll remove the draft status from this PR

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants