Polite lazy imports for Python package maintainers | PythonTest
Briefly

Polite lazy imports for Python package maintainers | PythonTest
"If you are a maintainer of a Python package, it's nice if you pay attention to the time it takes to import your package. Further, if you've got a Python package with multiple components where it's probable that many users will only use part of the package, then it's super nice if you set up your __init__.py files for lazy importing."
"The focus of the last article was on speeding up startup time for an application by delaying the import of dependencies until they are needed. However, what if you are building someone elses dependencies? Meaning, what if your package is used by other applications and packages? Then you can help out those applications and packages by making your own package super fast to import. That way, your package isn't the problem in their startup time or test collection time."
Package import time affects application startup and test collection. Developers can delay importing dependencies by moving imports into functions or using importlib.import_module and measuring with python -X importtime. Lazily importing package dependencies reduces initial overhead. For packages with multiple components, configuring __init__.py to lazily load subcomponents prevents unnecessary imports when only part of the package is used. Caching loaded components via __getattr__ in __init__.py allows on-demand loading and avoids repeated import costs. Making packages fast to import benefits downstream applications and test suites by removing a common source of startup latency.
Read at Pythontest
Unable to calculate read time
[
|
]