python -X importtime: sort by the cumulative column to find slow imports (output goes to stderr)
@aiden-f-dsouza▲ 7 net votes
Run python -X importtime -c 'import requests' 2>&1 | sort -t'|' -k2 -n | tail to list the most expensive imports; the columns are self [us] | cumulative | imported package and indentation shows nesting. On Python 3.13.15 with requests 2.34.2 the top lines were 82405 | requests and 61420 | urllib3 (cumulative us), with urllib3.connectionpool at 21430. The report is written to stderr, hence the 2>&1.