Skip to main content

Parse the output of `python -X importtime` and convert it to other formats.

Project description

Overview

This is a CLI tool and small library to parse the output of python -X importtime ... and convert it to other formats. Use it to help visualize and understand your Python import times—for example, by generating flame graphs.

Installation

With pip:

pip install importtime_convert
importtime-convert --help

Or with pipx:

pipx run importtime-convert --help

Usage as a CLI tool

The CLI tool is spelled importtime-convert or python -m importtime_convert. Give it the raw python -X importtime ... data on stdin, and it will output the converted result on stdout.

Here's a typical usage example, with Bash redirection syntax:

# Python outputs its raw importtime data to stderr. Save it to raw_importtime.txt.
python -X importtime -c 'import module_to_test' 2> raw_importtime.txt

# Convert it to "flamegraph.pl" format, and save that to converted_importtime.txt.
importtime-convert --output-format flamegraph.pl <raw_importtime.txt >converted_importtime.txt

Or, all in one step:

python -X importtime -c 'import module_to_test' \
    2>&1 >/dev/null \
    | importtime-convert --output-format flamegraph.pl \
    > converted_importtime.txt

See the available output formats below and importtime-convert --help for full details.

Usage as a library

The following are available from import importtime_convert:

parse(input: typing.TextIO | str) -> list[Import]

parse() takes the raw -X importtime data, as a string or as a file-like object. It returns the parsed import structure as a list of top-level imports. The list is in the order that the interpreter traversed them.

Import

An Import is a dataclass with the following keys:

  • package: str: The full package path of this import, e.g. "foo.bar".
  • cumulative_us: int: The time, in microseconds, that the interpreter spent on this module, including any subimports.
  • self_us: int: The time, in microseconds, that the interpreter spent on this module, not including any subimports.
  • subimports: list[Import]: This module's subimports. The list is in the order that the interpreter traversed them.

Output conversion functions

  • to_flamegraph_pl(imports: list[Import]) -> str
  • to_json_serializable(imports: list[Import]) -> list[dict[str, Any]]

See the available output formats below for details.

Available output formats

  • --output-format flamegraph.pl (CLI) / to_flamegraph_pl() (API)

    For flame graph generation tools.

    The format is defined by Brendan Gregg's flamegraph.pl script, but other flame graph tools accept it, too. (For example, https://www.speedscope.app/ and https://flamegraph.com/.)

  • --output-format json (CLI) / to_json_serializable() (API)

    A simple JSON format specific to this tool. Looks like this:

    [
      {
        "package": "foo",
        "self_us": 200,
        "cumulative_us": 300,
        "subimports": [
          {
            "package": "bar",
            "self_us": 100,
            "cumulative_us": 100,
            "subimports": []
          }
        ]
      },
      {
        "package": "baz.baz",
        "self_us": 100,
        "cumulative_us": 100,
        "subimports": []
      }
    ]
    

    See the API docs above for the meanings of the fields.

Feature requests and pull requests are welcome for additional output formats.

Related work and alternatives

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

importtime_convert-1.1.0.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

importtime_convert-1.1.0-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file importtime_convert-1.1.0.tar.gz.

File metadata

  • Download URL: importtime_convert-1.1.0.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for importtime_convert-1.1.0.tar.gz
Algorithm Hash digest
SHA256 6d4a806f40ed1f1a3ae05606c705fddab5299c740eea13ecc4d93d28c2cec975
MD5 7a0287f762393dbc2979b47b6017a44f
BLAKE2b-256 90efc48eb8470d84cd6520e2d02211172d709ccafead9efd744d5602eabd157b

See more details on using hashes here.

Provenance

The following attestation bundles were made for importtime_convert-1.1.0.tar.gz:

Publisher: deploy.yml on SyntaxColoring/importtime_convert

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file importtime_convert-1.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for importtime_convert-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 eef994362166d72e4847a5a1cd5de9c3b87c924a79c599145343e51627937085
MD5 e60f86e54a393c0e722acba6914adc3a
BLAKE2b-256 99c4a5c4efd3d5390fc41f7a17e9b28c286a66ce0ba1cf5d0d0e0cc9c3a3ab0e

See more details on using hashes here.

Provenance

The following attestation bundles were made for importtime_convert-1.1.0-py3-none-any.whl:

Publisher: deploy.yml on SyntaxColoring/importtime_convert

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page