Skip to content

Instantly share code, notes, and snippets.

@cb109
Last active November 10, 2025 10:04
Show Gist options
  • Save cb109/3105f77b0eb870b214f4f702443c6cfe to your computer and use it in GitHub Desktop.
Save cb109/3105f77b0eb870b214f4f702443c6cfe to your computer and use it in GitHub Desktop.
Analysing and improving Python import times

Run some command that initializes your codebase, e.g. for a Django project do:

time python -X importtime manage.py --version 2> importtimes.txt

Visualize and analyse contents of importtimes.txt with: https://kmichel.github.io/python-importtime-graph/

Defer imports by moving them inside functions/methods. With Python 3.15+ we will also be able to mark imports as lazy like lazy import xyz within the top level module scope, neat.

With deferring the largest/slowest few imports the startup time for any python commands can easily be halved or more, making everything noticeably faster!

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