Skip to content

Add -X importtime=2 for additional logging when an imported module is already loaded #102567

Closed

Description

@noahbkim

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: [will relink, need to do some paperwork].

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/

Linked PRs

Activity

  1. noahbkim commented on Nov 15, 2023

    @noahbkim
    ContributorAuthor

    @willingc Hey Carol, tagging you based on our recent conversation. We need some clarification on how to fill out the CLA before we can start contributing code to CPython; I'll quote the questions my colleague had directly (cc @tjstum):

    The form has a field for "bugs.python.org username." My understanding is that Python has ceased using this site and uses GitHub Issues and Pull Requests instead (the header on that website says the same). How should I fill out that field?

    Additionally, how do we indicate that a particular individual's GitHub account is associated with our entity's signed CLA? We have a GitHub organization, but individuals from our organization would be submitting Pull Requests using their own personal GitHub accounts. It wasn't totally clear to me how to go about this process.

    We tried reaching out to the PSF directly around the time this issue was created but didn't get a response. Thanks!

  2. vstinner commented on May 29, 2024

    @vstinner
    Member

    Can you paste an example of output before and after for comparison? You propose to add "cached" lines?

  3. added a commit that references this issue on May 6, 2025
  4. added a commit that references this issue on Jul 8, 2025
  5. added a commit that references this issue on Jul 8, 2025
  6. added a commit that references this issue on Jul 8, 2025
  7. added a commit that references this issue on Jul 11, 2025
  8. added 2 commits that reference this issue on Jul 12, 2025
  9. added a commit that references this issue on Jul 13, 2025
  10. added a commit that references this issue on Aug 4, 2025
  11. added a commit that references this issue on Aug 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions