Skip to main content

Show how long each Python import takes during startup

Project description

philiprehberger-import-profiler

Tests PyPI version Last updated

Show how long each Python import takes during startup.

Installation

pip install philiprehberger-import-profiler

Usage

Basic Profiling

from philiprehberger_import_profiler import profile_imports

report = profile_imports("my_package")

# Slowest imports
for entry in report.slowest(10):
    print(f"{entry.name}: {entry.duration_ms:.1f}ms")

# Tree view
report.print_tree(threshold_ms=5.0)
# └── my_package (124.5ms)
#     ├── requests (45.2ms)
#     │   └── urllib3 (22.1ms)
#     └── numpy (62.0ms)

# Summary
print(f"Total: {report.total_ms:.1f}ms, Modules: {report.module_count}")

Self Time and Export

report = profile_imports("my_package")

# Self time excludes children's duration
for entry in report.slowest(5):
    print(f"{entry.name}: total={entry.duration_ms:.1f}ms self={entry.self_ms:.1f}ms")

# Export as list of dicts
data = report.to_dict()
# [{"name": "requests", "duration_ms": 45.2, "self_ms": 23.1, "parent": "my_package"}, ...]

Sorting and filtering reports

report = profile_imports("my_package")

# Top N slowest entries (descending by duration_ms)
for entry in report.slowest(3):
    print(f"{entry.name}: {entry.duration_ms:.1f}ms")

# Filter to entries whose module name starts with a prefix
numpy_only = report.filter("numpy")
print(f"numpy.* modules: {numpy_only.module_count}, total={numpy_only.total_ms:.1f}ms")

# filter returns a new report; the original is unchanged
assert report.module_count >= numpy_only.module_count

API

Function / Class Description
profile_imports(module_name) Profile all imports, returns ImportReport
report.slowest(n=10) Top N slowest imports as ImportEntry list, sorted descending
report.filter(prefix) New ImportReport containing only entries whose name starts with prefix
report.print_tree(threshold_ms=0) Print indented tree
report.total_ms Total import time in milliseconds
report.module_count Number of modules imported
report.to_dict() Export as list of dicts
ImportEntry.name Module name
ImportEntry.duration_ms Total duration including children
ImportEntry.self_ms Duration excluding children

Development

pip install -e .
python -m pytest tests/ -v

Support

If you find this project useful:

Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

License

MIT

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

philiprehberger_import_profiler-0.2.0.tar.gz (182.2 kB view details)

Uploaded Source

Built Distribution

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

File details

Details for the file philiprehberger_import_profiler-0.2.0.tar.gz.

File metadata

File hashes

Hashes for philiprehberger_import_profiler-0.2.0.tar.gz
Algorithm Hash digest
SHA256 dd3ef7a974303dbd63297f50b7185f7d9952824c707063dee5be8f8d90e63252
MD5 0bccf5e4dd9f85354d8d01d039de77c7
BLAKE2b-256 8c87e4152072ac916759286ae462404d3682040a1225c0c8105b7f89adc91e00

See more details on using hashes here.

File details

Details for the file philiprehberger_import_profiler-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_import_profiler-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f1572b0662f4be674b697605da4f374dca71dd800e16d86876892c2c201c5087
MD5 fb7f1a1e1bb2a71445829023048f48a4
BLAKE2b-256 e4824178817ebc114c405689c08cae89d59850d8c1d0cffbcdeb094e5f295ec2

See more details on using hashes here.

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