philiprehberger-import-profiler
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"}, ...]
Summary and JSON export
Use summary() for a compact dict you can log or pretty-print, and to_json() for the full entry list as a JSON string.
report = profile_imports("my_package")
print(report.summary(slowest=3))
# {"total_ms": 124.5, "module_count": 42, "slowest": [
# {"name": "numpy", "duration_ms": 62.0},
# {"name": "requests", "duration_ms": 45.2},
# {"name": "urllib3", "duration_ms": 22.1},
# ]}
# JSON string (compact when indent=None)
print(report.to_json(indent=None))
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 |
report.to_json(indent=2) |
Export entries as a JSON-formatted string |
report.summary(slowest=5) |
Compact dict with total_ms, module_count, and top-N slowest |
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:
License
Release files for philiprehberger-import-profiler 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| philiprehberger_import_profiler-0.3.0.tar.gz | 183.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| philiprehberger_import_profiler-0.3.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 189.1 kB
Release files / philiprehberger_import_profiler-0.3.0.tar.gz
| Download URL | philiprehberger_import_profiler-0.3.0.tar.gz |
|---|---|
| Size | 183.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
dcce53e736c4efec645d8a514eed5d9d8fe91c02c985edf2d43dddbf9833bb3f
|
|
BLAKE2b-256 checksum How to use checksums |
3a0e91e8fd7a2cd2ed0a65a1c335231d4002845de809f0a012e5d5c20be6309e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.13
|
Release files / philiprehberger_import_profiler-0.3.0-py3-none-any.whl
| Download URL | philiprehberger_import_profiler-0.3.0-py3-none-any.whl |
|---|---|
| Size | 6.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
add2febe4457a5bf38310ae95984ec102a5ec14f817a782dccdef32059f967f9
|
|
BLAKE2b-256 checksum How to use checksums |
ddd4bfeaf625e876088fbd1082344d8dd5ffc8d14886e59055fff23cc84d37df
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.13
|