New GitHub issue #102567 from noahbkim:<br>

<hr>

<pre>
# Feature or enhancement

Add special handling for `-X importtime=2` that provides additional output when already-loaded modules are imported. This will allow users to get a complete picture of runtime imports.

# Pitch

While `-X importtime` is incredibly useful for analyzing module import times, by design, it doesn't log anything if an imported module has already been loaded. `-X importtime=2` would provide additional output for every module that's already been loaded:

```
>>> import uuid
import time: cached    | cached     |   _io
import time: cached    | cached     |   _io
import time: cached    | cached     |   os
import time: cached    | cached     |   sys
import time: cached    | cached     |   enum
import time: cached    | cached     |     _io
import time: cached    | cached     |     _io
import time: cached    | cached     |     collections
import time: cached    | cached     |     os
import time: cached    | cached     |     re
import time: cached    | cached     |     sys
import time: cached    | cached     |     functools
import time: cached    | cached     |     itertools
import time:       151 |        151 |     _wmi
import time:     18290 |      18440 |   platform
import time:       372 |        372 |   _uuid
import time:     10955 |      29766 | uuid
```

In codebases with convoluted/poorly managed import graphs (and consequently, workloads that suffer from long import times), the ability to record all paths to an expensive dependency–not just the first-imported–can help expedite refactoring (and help scale identification of this type of issue). More generally, this flag would provide a more efficient path to tracking runtime dependencies.

The changes required are largely unintrusive: here's my reference implementation: https://github.com/python/cpython/compare/main...noahbkim:cpython:feature/importcache

# Previous discussion

Discussion: https://discuss.python.org/t/x-importtrace-to-supplement-x-importtime-for-loaded-modules/23882/5
Prior email chain: https://mail.python.org/archives/list/python-ideas@python.org/thread/GEISYQ5BXWGKT33RWF77EOSOMMMFUBUS/
</pre>

<hr>

<a href="https://github.com/python/cpython/issues/102567">View on GitHub</a>
<p>Labels: type-feature</p>
<p>Assignee: </p>