In terms of “always imported” modules, I haven’t seen anything that would remove any of these for 3.14, can’t check right this moment, but 3.13:
3.13, python -SsuB -X importtime -c "pass"
, which is optimistic about people not needing site customization and knowing that disabling it can speed things up slightly when unneeded.
Windows:
import time: self [us] | cumulative | imported package
import time: 84 | 84 | winreg
import time: 169 | 169 | _io
import time: 54 | 54 | marshal
import time: 177 | 177 | nt
import time: 2773 | 3172 | _frozen_importlib_external
import time: 328 | 328 | time
import time: 870 | 1197 | zipimport
import time: 40 | 40 | _codecs
import time: 1333 | 1373 | codecs
import time: 1601 | 1601 | encodings.aliases
import time: 4440 | 7414 | encodings
import time: 477 | 477 | encodings.utf_8
import time: 42 | 42 | _signal
import time: 23 | 23 | _abc
import time: 737 | 760 | abc
import time: 685 | 1444 | io
import time: 1231 | 1231 | linecache
These imports (trimmed from a full output) represent site customization with virtual environment usage, which I expect to be nearly universal.
import time: 52 | 52 | _stat
import time: 202 | 253 | stat
import time: 756 | 756 | _collections_abc
import time: 67 | 67 | genericpath
import time: 79 | 79 | _winapi
import time: 307 | 453 | ntpath
import time: 755 | 2215 | os
import time: 101 | 101 | _sitebuiltins
import time: 1075 | 1075 | encodings.utf_8_sig
import time: 670 | 670 | __future__
import time: 1433 | 2103 | _virtualenv
import time: 284 | 284 | sitecustomize
import time: 2196 | 7972 | site
These are mostly the same on linux, so I won’t be duplicating them. The windows specific modules winreg, nt and ntpath (the last only with site customization) aren’t pulled in, in their place posix is pulled in, with posixpath and erno being pulled in with site customization.
re
appears to be nearly guaranteed to be pulled in currently in practice because of distutils templates, but that’s possible to avoid invoking with -m module
since runpy doesn’t import re directly or indirectly.
Without a generalized speedup for module import and execution times and a generalized laziness mechanism for imports that can be used within the standard library I tend to agree that there are certain modules where the effort would be better spent making the module faster to import rather than trying to lazily import it, but there are quite a few where I think there’s an extreme opposite at play. For example, the typing module. It’s extremely slow when considering that it is a module that most people don’t have any runtime use of; If the entire module could be made lazy, this would likely be an improvement for the majority of applications with any in-line type hints in any dependency.
This is probably going to require some amount of informed choices on which approaches best apply to each module outside of the generalized improvements that every module benefits from.