Lazy Import
Installation
pip install lz-import
# Or
poetry add lz-import
Usage 1
# File: file_takes_long_time_to_import.py
init = initializer()
class Module:
...
print("imported!")
from lazy_import import lazy_import
with lazy_import():
from file_takes_long_time_to_import import Module # Not imported yet
def run():
Module()
run() # Now Module is imported.
# print imported!
Module will be imported when the __call__ or __getattr__ methods are called.
See tests/test_load_later.py
Usage 2
# File: company.py
from lazy_import import lazy_import
with lazy_import():
from user import User
class Company:
name = "company"
def get_user(self) -> User:
return User()
# File: user.py
from lazy_import import lazy_import
with lazy_import():
from company import Company
class User:
name = "user"
def get_company(self) -> Company:
return Company()
if __name__ == "__main__":
company = User.get_company()
User will be imported when the __call__ or __getattr__ methods are called.
This example codes are implemented in the tests folder. See tests/test_user.py and tests/test_company.py.
NOTE
- Keep in mind that the class of lazy imported is not the same class with your original
Userclass. It is wrapped by another class inside oflazy_import(). - Only work for module or class.
TODO
This library currently doesn't support follow syntax:
with lazy_import():
# these are actually possible but currently not implmented...
import user
from user import User as user
How it works?
We can find out which files to import by parsing bytecodes inside the with syntax. After that, just wrap the value to be imported.
Metadata
Release files for lz-import 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| lz_import-0.1.2.tar.gz | 4.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| lz_import-0.1.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 9.7 kB
Release files / lz_import-0.1.2.tar.gz
| Download URL | lz_import-0.1.2.tar.gz |
|---|---|
| Size | 4.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
33a7c266e89804c3120c8faebe82e750edf5aa135f1525596d5852422e7a3407
|
|
BLAKE2b-256 checksum How to use checksums |
9b3e2e5d4eb3825c076b50a8e595d9dc58c1d138e26fb71e72642957187b0bb4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.4.2 CPython/3.11.3 Linux/5.15.0-1037-azure
|
Release files / lz_import-0.1.2-py3-none-any.whl
| Download URL | lz_import-0.1.2-py3-none-any.whl |
|---|---|
| Size | 5.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5355893168d32a083c10c6a2fd604153e34173d37e320cd8b2bf787298b02262
|
|
BLAKE2b-256 checksum How to use checksums |
af23e7e58bdbc82f18466f798afd7c346cbc42a387ef33cbb7a70aa18da3b8d9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.4.2 CPython/3.11.3 Linux/5.15.0-1037-azure
|