Skip to main content

Show how long each Python import takes during startup

Project description

philiprehberger-import-profiler

Tests PyPI version License

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"}, ...]

API

Function / Class Description
profile_imports(module_name) Profile all imports, returns ImportReport
report.slowest(n) Top N slowest imports as ImportEntry list
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

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.1.7.tar.gz (4.7 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.1.7.tar.gz.

File metadata

File hashes

Hashes for philiprehberger_import_profiler-0.1.7.tar.gz
Algorithm Hash digest
SHA256 d92f9179848ad78881f170b1cfa21cb3b44ed2263f093692eb7f8b5ddb3f0465
MD5 6876a045a29531dd9b4ec46bfb4ec334
BLAKE2b-256 2bd395070d157dbc2932a5fa47f50b46def09372c6c9515da8d734af2d50ae56

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_import_profiler-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 a1e383e0abc97c78621859f6ce98f48a2e0e44b91877dc81d0e1284374b4997e
MD5 3ffd4e8e81ff46876b369e6b11c75114
BLAKE2b-256 5a1877fb01a4bbe8d4cfef45f38acd947763f33ec8b77b9b3a65105d54f70391

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