Lazy optional imports
Project description
lazyshell
Lazy optional imports for Python projects. shell_import lets you declare optional dependencies once and load them on first use. Missing packages can degrade gracefully using sink proxies so your code keeps running.
Installation
pip install lazyshell
Supports Python 3.9 and newer and has no runtime dependencies.
Quick start
from lazyshell import shell_import
# request multiple modules at once
np, hass, missing = shell_import(
"numpy",
("hass", "homeassistant.core"),
"not_installed",
sink=True, # return sink proxies for missing packages
sink_map={"hass.log": print}, # route hass.log -> print
)
# modules are imported on first use
arr = np.arange(3)
# sink proxies are truthy and return None by default
missing.anything() # -> None
# override behaviour for a specific attribute
hass.log("hello") # prints "hello"
Proxy behaviour
bool(proxy)isFalseuntil the real module or object is successfully imported.- Accessing an attribute or calling the proxy triggers the import.
- If the import fails:
- with
sink=False(default) aMissingPackagesentinel is returned and attribute access raisesImportError. - with
sink=TrueaSinkProxyis returned. It is truthy, attribute access returns further sink proxies and method calls are no-ops unless a fallback is provided viaset.
- with
- The
.with_sink()helper enables sink mode on creation;.enable_sink()enables it after a failed import. - Register fallbacks with
proxy.set("attr", value)or via attribute proxies:proxy.attr.set(value). - The
is_loadedproperty reports whether the underlying import has occurred.
Set the environment variable LAZYSHELL_DEBUG=1 to get a one-time warning when a sink proxy is instantiated.
License
lazyshell is distributed under the terms of the MIT License. See LICENSE for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file lazyshell-0.2.1.tar.gz.
File metadata
- Download URL: lazyshell-0.2.1.tar.gz
- Upload date:
- Size: 5.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
65061346cb939366cc61aded7e5518fa3fc0c789614c1e0be0b0722c934863c9
|
|
| MD5 |
f59c3510f2be4ccd077e58d02d8904df
|
|
| BLAKE2b-256 |
fe46b0942d2c396f58b833ebcf45658d3f9895ef256ace3ca89fe3f50184a06b
|
File details
Details for the file lazyshell-0.2.1-py3-none-any.whl.
File metadata
- Download URL: lazyshell-0.2.1-py3-none-any.whl
- Upload date:
- Size: 6.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
711f0ded4c43e36f0aa74a6d65efa959a8d9084a941bdbf0a9cfc631aba305ae
|
|
| MD5 |
32a4311dce8de941650d1b94589e2b21
|
|
| BLAKE2b-256 |
4c10df579ef46738bf266e4d08393cdeefcfa972cd9db0e2f8672a33965ada8c
|