Skip to main content

A lightweight and extensible way to implement lazy imports globally.

Project description

Lazi: Lazy Imports Everywhere

An easy way to implement and track lazy imports globally.

No external dependencies.

Requres Python 3.11.

Usage:

poetry add lazi
TRACE=1 python3
Python 3.11.2
>>> import lazi.auto
>>> from django import test
[139840712693360] LAZY >>>> [139840715383056] django[.test]
[139840712694640] LAZY <<<< [139840712694240] django.utils[.version] = [139840712695040]
[139840712695040] LAZY >>>> [139840712694880] [django.utils.]django.utils.version[.get_version]
[139840712696720] LAZY >>>> [139840712696000] [django.utils.]django.utils.regex_helper[._lazy_re_compile]
[139840712697680] LAZY >>>> [139840712697280] [django.utils.]django.utils.functional[.SimpleLazyObject]
>>> test.TestCase
# ... A lot of output ...
<class 'django.test.testcases.TestCase'>
>>> _

Use for specific modules:

>>> from lazi.core import lazi
>>> with lazi:
...   import django
...   print(django.VERSION)
... 
(4, 2, 1, 'final', 0)
>>> _

Or:

>>> from lazi.core import lazy
>>> django = lazy("django")
>>> django.VERSION
(4, 2, 1, 'final', 0)
>>> _

Configuration

The lazi.conf namespace package contains configuration modules that get autoloaded (in import order) by lazi.conf.conf. It is fully decoupled from the rest of the codebase.

As a result, it's possible configure Lazi by creating lazi.conf modules in your project (within the lazi.conf namespace package), and use conf modules provided by other packages.

Configuration is not yet controllable via environment variables, but this is planned for the future. Update: Supported for DEBUG_TRACING.

It's also possible to manually change the configuration at runtime, with the caveat that some variables may have already been used by lazi.core. To avoid this, configure Lazi before importing it:

from lazi.conf import conf

conf.TRACE = 1
import lazi.auto
# ...

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lazi-1.8.119.tar.gz (25.6 kB view hashes)

Uploaded Source

Built Distribution

lazi-1.8.119-py3-none-any.whl (26.0 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page