Skip to main content

lazypkg: lazy import modules and subpackages

Version_status license

What is lazypkg?

lazypkg features the LazyPkg object, a ModuleType object that lazy imports subpackages and allows access to subpackage objects. Lazy importing is when a submodule is not imported along with the top level module, but is still accessible on demand.

Installation

Get the latest version of lazypkg from https://pypi.python.org/pypi/lazypkg/

If you have an installation of Python with pip, simple install it with:

$ pip install lazypkg

To get the git version, run:

$ git clone git://github.com/yoelcortes/lazypkg

Getting started

LazyPkg objects are straight forward. First assume the following package structure:

package/
  __init__.py
  module.py
  ...
  subpackage/
    __init__.py
    submodule.py
    ...

Here is an example implementation of a LazyPkg object in the package __init__.py file:

from lazypkg import LazyPkg
from .module import obj
__all__ = ['obj']
LazyPkg(__name__, ['subpackage'])
# This converts the package into a LazyPkg object and lazy imports 'subpackage'

Say the subpackage __init__.py file looks like this:

from . import submodule
__all__ = ['submodule']
print('imported subpackage')

When the subpackage is accessed, only then will it be imported:

>>> import package # Subpackages are not imported
>>> package.obj
# -> Works just like an ordinary package
>>> package.subpackage
imported subpackage

Additionally, subpackage object are directly accessible:

>>> import package
>>> package.submodule
# -> Assuming "submodule" is not defined in the top level package,
#    this will import and search subpackages for the "submodule"

This is all possible because the package become a LazyPkg instance:

>>> import package
>>> type(package)
lazypkg.LazyPkg

Bug reports

To report bugs, please use the lazypkg’s Bug Tracker at:

https://github.com/yoelcortes/lazypkg

License information

See LICENSE.txt for information on the terms & conditions for usage of this software, and a DISCLAIMER OF ALL WARRANTIES.

Although not required by the lazypkg license, if it is convenient for you, please cite lazypkg if used in your work. Please also consider contributing any changes you make back, and benefit the community.

Citation

To cite lazypkg in publications use:

Yoel Cortes-Pena. lazypkg: lazy import modules and subpackages.
https://github.com/yoelcortes/lazypkg

Metadata

Release files for lazypkg 1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for lazypkg 1.4
File Size Uploaded
lazypkg-1.4.tar.gz 3.5 kB Details

Release files / lazypkg-1.4.tar.gz

Download URL lazypkg-1.4.tar.gz
Size 3.5 kB
Tags Source
SHA-256 checksum
How to use checksums
80aac786f902cf5468401a070186fff0e587d2fba5c24cb4c0dbbb074bb8c9a6
BLAKE2b-256 checksum
How to use checksums
18fad903b770526912c5c84d97d25d1798ed09474f7c2a6399964ac50563d497
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via Python-urllib/3.6

Release history Release notifications | RSS feed

This release

1.4 This release

1 release file

1.3

1 release file

1.2

1 release file

1.1

1 release file

1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page