Skip to main content

Tool to support lazy imports

Project description

Lazy-Imports

Apache-2.0 License Python Version pypi
pytest Static Code Checks GitHub issues

This is a Python tool to support lazy imports. Likewise, the actual initialization of the module does not occur until usage time to postpone ModuleNotFoundErrors to the time of the actual usage of the module. This is useful when using various optional dependencies which might not all be installed or which have high load times and/or ressource consumption.

Table of Contents

Maintainers

One Conversation
This project is maintained by the One Conversation team of Deutsche Telekom AG. It is based on _LazyModule from HuggingFace and try_import() from the Optuna framework. Many thanks to HuggingFace for your consent and to Optuna for your consent to publish it as a standalone package 🤗 ♥.

Installation

Lazy-Imports is available at the Python Package Index (PyPI). It can be installed with pip:

$ pip install lazy-imports

Usage & Example for LazyImporter

A good and easy to understand example of how to use Lazy-Imports can be found in the __init__.py file of the HPOflow package. It is printed here:

import sys
from typing import TYPE_CHECKING

from lazy_imports import LazyImporter

from hpoflow.version import __version__


_import_structure = {
    "mlflow": [
        "normalize_mlflow_entry_name",
        "normalize_mlflow_entry_names_in_dict",
        "check_repo_is_dirty",
    ],
    "optuna": ["SignificanceRepeatedTrainingPruner"],
    "optuna_mlflow": ["OptunaMLflow"],
    "optuna_transformers": ["OptunaMLflowCallback"],
    "utils": ["func_no_exception_caller"],
}

# Direct imports for type-checking
if TYPE_CHECKING:
    from hpoflow.mlflow import (  # noqa: F401
        check_repo_is_dirty,
        normalize_mlflow_entry_name,
        normalize_mlflow_entry_names_in_dict,
    )
    from hpoflow.optuna import SignificanceRepeatedTrainingPruner  # noqa: F401
    from hpoflow.optuna_mlflow import OptunaMLflow  # noqa: F401
    from hpoflow.optuna_transformers import OptunaMLflowCallback  # noqa: F401
    from hpoflow.utils import func_no_exception_caller  # noqa: F401
else:
    sys.modules[__name__] = LazyImporter(
        __name__,
        globals()["__file__"],
        _import_structure,
        extra_objects={"__version__": __version__},
    )

Usage & Example for try_import

try_import is a context manager that can wrap imports of optional packages to defer exceptions. This way you don't have to import the packages every time you call a function, but you can still import the package at the top of your module. The context manager defers the exceptions until you actually need to use the package. You can see an example below:

from lazy_imports import try_import

with try_import() as optional_package_import:  # use try_import as a context manager 
    import optional_package  # optional package that might not be installed

# other non optional functions here    
    
def optional_function():  # optional function that uses the optional package
    optional_package_import.check()  # check if the import was ok or raise a meaningful exception
    
    optional_package.some_external_function()  # use the optional package here

Support and Feedback

The following channels are available for discussions, feedback, and support requests:

Licensing

Copyright (c) 2021 Philip May, Deutsche Telekom AG
Copyright (c) 2020, 2021 The HuggingFace Team
Copyright (c) 2018 Preferred Networks, Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

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_imports-0.2.1.tar.gz (11.0 kB view details)

Uploaded Source

Built Distribution

lazy_imports-0.2.1-py3-none-any.whl (11.7 kB view details)

Uploaded Python 3

File details

Details for the file lazy_imports-0.2.1.tar.gz.

File metadata

  • Download URL: lazy_imports-0.2.1.tar.gz
  • Upload date:
  • Size: 11.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.9.6

File hashes

Hashes for lazy_imports-0.2.1.tar.gz
Algorithm Hash digest
SHA256 f8e7abcd8293f0304a7c03df69eacbfba5cb67310e00a09a53adf4c2813015da
MD5 a14cfd7cb27d7f145207a93d51541efd
BLAKE2b-256 539ea62260b2330fed7694558918b80d898ba7577078dd5618546e2b2a9188a9

See more details on using hashes here.

File details

Details for the file lazy_imports-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: lazy_imports-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 11.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.9.6

File hashes

Hashes for lazy_imports-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 deb9cdecc3467ddffbaab86d64015116051d94cd6c14bc8fc1f4d32edd0a81b9
MD5 46832f4af357ad1f23d90e982695e709
BLAKE2b-256 8b07c3b4f378344aaea08e0e0ca099c4bd3bd717e55f2fed5f017d28431d1cb6

See more details on using hashes here.

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