Skip to main content

How to Find Slow Imports in Python with -X importtime

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

When a Python program takes a long time to start, the imports are often where the time goes. Python can tell you exactly which ones: run it with -X importtime and it reports how long every module took to import. This guide reads that report for import json on Python 3.15.2, then sorts it to find the slowest modules.

Syntax​

Syntax
python -X importtime <program>
PartWhat it means
-X importtimeAn option for Python itself. It must come straight after python, before the program
<program>What to run and time: a script, such as main.py, or code given with -c "..."

The Python documentation describes it: "-X importtime to show how long each import takes. It shows module name, cumulative time (including nested imports) and self time (excluding nested imports)." The report goes to standard error, the stream programs use for messages, not to standard output, where print writes.

The Example​

import json, one line of code that imports Python's JSON module and the modules it needs, run in a uv project made with uv init.

Prerequisites​

  • uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2.

Step 1: Time an Import​

Terminal
uv run python -X importtime -c "import json"
Output
import time: self [us] | cumulative | imported package
import time: 103 | 103 | _io
import time: 22 | 22 | marshal
import time: 133 | 133 | posix
import time: 329 | 586 | _frozen_importlib_external
import time: 512 | 512 | time
import time: 145 | 656 | zipimport
import time: 32 | 32 | _codecs
import time: 383 | 415 | codecs
import time: 390 | 390 | encodings.aliases
import time: 627 | 1431 | encodings
import time: 159 | 159 | encodings.utf_8
import time: 46 | 46 | _signal
import time: 21 | 21 | _abc
import time: 101 | 121 | abc
import time: 28 | 28 | _stat
import time: 64 | 91 | stat
import time: 530 | 530 | _collections_abc
import time: 39 | 39 | errno
import time: 57 | 57 | genericpath
import time: 84 | 179 | posixpath
import time: 303 | 1222 | os
import time: 80 | 80 | _sitebuiltins
import time: 617 | 617 | encodings.utf_8_sig
import time: 285 | 285 | sitecustomize
import time: 842 | 3044 | site
import time: 63 | 63 | linecache
import time: 22 | 22 | _types
import time: 191 | 213 | types
import time: 804 | 1016 | enum
import time: 37 | 37 | _sre
import time: 149 | 149 | re._constants
import time: 203 | 351 | re._parser
import time: 69 | 69 | re._casefix
import time: 216 | 672 | re._compiler
import time: 57 | 57 | itertools
import time: 88 | 88 | keyword
import time: 37 | 37 | _operator
import time: 160 | 196 | operator
import time: 121 | 121 | reprlib
import time: 34 | 34 | _collections
import time: 586 | 1080 | collections
import time: 32 | 32 | _functools
import time: 347 | 1457 | functools
import time: 105 | 105 | copyreg
import time: 445 | 3694 | re
import time: 466 | 466 | _json
import time: 216 | 681 | json.scanner
import time: 395 | 4769 | json.decoder
import time: 206 | 206 | json.encoder
import time: 651 | 5625 | json

The terminal after uv run python -X importtime -c "import json": 51 lines, a header with self, cumulative and imported package, Python's start-up modules such as encodings, os and site, then re, json.scanner, json.decoder and json.encoder, and json last with a cumulative 5625 microseconds.

Each line is one module, and the header names the columns:

  • self [us]: the time spent in that module alone, in microseconds, millionths of a second.
  • cumulative: that time plus every module it imported in turn.
  • imported package: the module's name. A module is printed after the modules it imported, and those are indented further.

The last line is json: 5,625 microseconds in all, about 5.6 milliseconds. Its own share was 651, and most of the rest was json.decoder (4,769), which imported re, Python's regular expressions. The lines down to site are Python's own start-up, before import json runs. sitecustomize among them is a module Python looks for at start-up: the site documentation says "an attempt is made to import a module named sitecustomize". There isn't one here, and -X importtime lists an import even when it fails. The command works unchanged in PowerShell.

Step 2: Save the Report and Find the Slowest Modules​

A large package prints thousands of lines, so save the report to a file and sort it by the cumulative column:

Terminal
uv run python -X importtime -c "import json" 2> imports.txt
sort -t'|' -k2 -n imports.txt | tail -5
Output (macOS)
import time: 328 | 1378 | functools
import time: 388 | 1933 | site
import time: 300 | 3260 | re
import time: 258 | 4158 | json.decoder
import time: 187 | 4549 | json

The terminal after saving the report to imports.txt and sorting it: the five imports with the largest cumulative times, functools 1378, site 1933, re 3260, json.decoder 4158 and json 4549 microseconds.

2> imports.txt sends standard error, where the report goes, into a file: 51 lines here, and nothing on the screen. Then the sort:

  • macOS and Linux: sort -t'|' -k2 -n splits each line at | and sorts by the second part, the cumulative time, as numbers. tail -5 keeps the last five lines, the slowest.
  • PowerShell: Select-Object -Skip 1 skips the header, Sort-Object sorts by the number between the first and second |, and Select-Object -Last 5 keeps the slowest five. It printed the same five modules.

Here the slowest line is json, the module the code imported, because its time includes everything it imported. The lines just above it show where that time went: json.decoder, and re inside it.

Common Mistakes​

MistakeWhat happensDo this instead
Saving the report with >The report still printed on the screen, and imports.txt stayed empty: 0 bytes. > saves standard output, and the report is on standard errorUse 2>
Putting the option after the program: python -c "import json" -X importtimeNothing printed. Everything after the program goes to the program as its own arguments, so Python never saw the optionPut it straight after python

FAQ​

Why do the numbers change between runs?​

Timings vary a little every time. json took 5,625 microseconds in Step 1 and 4,549 in the run saved in Step 2. Compare large differences, not small ones.

Can I turn it on without changing the command?​

Yes, with the PYTHONPROFILEIMPORTTIME environment variable. The documentation: "This is equivalent to setting the -X importtime option." PYTHONPROFILEIMPORTTIME=1 uv run python -c "import json" printed the same report. In PowerShell, set it first with $env:PYTHONPROFILEIMPORTTIME = "1".

Conclusion​

python -X importtime -c "import json" reported every module the import loaded, with its own time and its total. Saved with 2> and sorted by the cumulative column, the report put json and json.decoder at the top.

Additional Resources​