Skip to main content

benchmark-imports

A CLI tool to record how much time it takes to import each dependency in a Python project. Use it when the import time for your code matters.

example of the program output

Usage

Install:

python3 -m pip install benchmark-imports

Use:

python3 -m benchmark_imports my_module_name

For example, measure import time for numpy:

python3 -m pip install numpy
python3 -m benchmark_imports numpy

Troubleshooting

  • To be able to import your module and all its dependencies, the tool has to be installed in the same environment as the tested module and all its dependencies.
  • Keep in mind that the tool actually imports and executes the given module and all its dependencies. So, don't run it on untrusted code or code that can break something at import time.
  • The tool will report you at the end the list of errors that occured in some modules when importing them but were suppressed by other modules. It doesn't mean something is broken. For example, it can indicate that the library has an optional dependency that it tries to import but just ignores if unavailable. However, that means that on some environment the module will be successfully imported, and so the import time may be different.

Improving import time

When you identified the slow modules, this is what you can do:

  • Decrease coupling. "A little copying is better than a little dependency". For example, if you import numpy only to use a single small function (like numpy.sign), just implement the function by yourself.
  • Use local imports. The best practice is to have imports on the top-level of the file. However, if this is a slow module that is used only in one function which isn't called too often, just move the import into the function body. It won't make the function much slower (well, except when you call it in the first time) because Python caches all imports in sys.modules.
  • Use lazy imports. The idea is about the same as with function-local imports: the module will be actually imported and executed only when you try to use it in the first time. It can be achieved either with deferred-import library or Implementing lazy imports snippet from the importlib documentation.
  • Make type annotations lazy by adding from __future__ import annotations at the beginning of each file (see PEP 563).
  • If something is imported only to be used in type annotations, move the import inside if TYPE_CHECKING block.

And as a general rule, don't optimize something until you prove it is slow. This is why this tool exists.

Module type

For each reported module, the tool will show you one of the following types:

  • root is the original module you benchmark (or one of its parent modules).
  • project is a child module of the root module.
  • dependency is a direct dependency of one of the modules in the project.
  • transitive is a dependency of one of the dependencies.

Release files for benchmark-imports 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for benchmark-imports 1.0.0
File Size Uploaded
benchmark-imports-1.0.0.tar.gz 202.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for benchmark-imports 1.0.0
File Interpreter ABI Platform
benchmark_imports-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 209.9 kB

Release files / benchmark-imports-1.0.0.tar.gz

Download URL benchmark-imports-1.0.0.tar.gz
Size 202.6 kB
Tags Source
SHA-256 checksum
How to use checksums
630892f436b1925325d701d990b9504629c7f53532ff361ec93f1b4ad4f32ff0
BLAKE2b-256 checksum
How to use checksums
b684571c49b3dfd3bcc693cb373940a15c376be5794cebf273469486dae2be87
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.25.1

Release files / benchmark_imports-1.0.0-py3-none-any.whl

Download URL benchmark_imports-1.0.0-py3-none-any.whl
Size 7.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0162e1071c17d092528743bc685cef4c203ceecf35e58ea4c48ddf88a3aebefa
BLAKE2b-256 checksum
How to use checksums
120add95ce6e26297f292b51a7da5307f75fddf9039fe842c14d806f36341032
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.25.1

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page