Skip to main content

A simple lazy module loader for python

Project description

lazy-loading

lazy-loading is a simple lazy module loader for python.

Usage:

First, you'll need to import lazy-loading.

To lazily load a package, use:

lazy-loading.lazyload("package")

This will inject "package" into your global variables, meaning you can use package like usual.

[!TIP] If you don't want to inject into globals, you can set the inject parameter as such:

package = lazy-loading.lazyload("package", False)

lazyload will return the same LazyModule object it would inject into your global variables.

Note: lazyload will return the object even if inject is True.

You can lazily load submodules of your package:

lazy-loading.lazyload("mypacakge.submodule")

[!IMPORTANT] Relative imports (e.g. .submodule) are not supported. You need to use absolute module paths.

IDE autocompletion & type-checking

To get type-checking and autocompletion, you need to import all lazy-loaded packages if typing.TYPE_CHECKING is True:

from lazy-loading import lazyload
from typing import TYPE_CHECKING

lazyload("package1")
lazyload("package2")

if TYPE_CHECKING:
    import package1
    import package2

This will only run if a type-checker or linter is checking your code and will not cause packages to not lazy load in normal contexts.

Limitations:

Partial lazy loading

Partial lazy loading (e.g. from package import symbol) is not supported. Accessing an attribute or function will automatically import the entire module.

Relative imports

As mentioned previously, relative imports are not supported. You can lazy-load modules in your script's directory:

lazy-loading.lazyload("module")

If you are building a package:

lazy-loading.lazyload("package.module")

Lazily-loaded module dependencies

Imports called by lazily-loaded modules will not be lazy-loaded (unless the module uses lazy loading itself.)

Global name injection

In unusual contexts, global name injection may fail. To prevent this, you can assign the returned value of lazyload to a variable and possibly disable injection.

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

lazy_loading-0.1.0.post2.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lazy_loading-0.1.0.post2-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file lazy_loading-0.1.0.post2.tar.gz.

File metadata

  • Download URL: lazy_loading-0.1.0.post2.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.17 {"installer":{"name":"uv","version":"0.9.17","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Fedora Linux","version":"43","id":"","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for lazy_loading-0.1.0.post2.tar.gz
Algorithm Hash digest
SHA256 416e2fe8142384b9c6baae6856107f334f8dbddda57c4cca0f02555a5627c9db
MD5 9743f53dd171fd32360b97e93accdbec
BLAKE2b-256 72ba0dd92da64af32e3f85439361aa76275210740a7f3026f00e6f231fa0c297

See more details on using hashes here.

File details

Details for the file lazy_loading-0.1.0.post2-py3-none-any.whl.

File metadata

  • Download URL: lazy_loading-0.1.0.post2-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.17 {"installer":{"name":"uv","version":"0.9.17","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Fedora Linux","version":"43","id":"","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for lazy_loading-0.1.0.post2-py3-none-any.whl
Algorithm Hash digest
SHA256 f0694894ffa2dd850f397cc7d4135d36c3203a1f5fa44a3e3167c6a3e17e9fbf
MD5 c4d9ee85a537c66ccc75048decddc4d4
BLAKE2b-256 158d2ae4fe4931e6d2f496dbdd94acb492d8bf960dba6d5414143c5e5f90573e

See more details on using hashes here.

Supported by

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