tf-lazy-loader
Installation
pip install tf-lazy-loader
Usage
from tf_lazy_loader import dynamic_import
os = dynamic_import("os")
print(os.environ)
Conditionally switching between lazy and eager imports
You can set the lazy
arg of the dynamic_import
function to False
to eagerly import the given module. This can be useful if you only want to perform lazy imports based on a flag.
For example, if you have a Django project and you only want to enable lazy imports when in DEBUG mode, and do imports eagerly in production, you would do something like this:
from django.conf import settings
DEBUG = settings.DEBUG
os = dynamic_import("os", lazy=DEBUG)