Package: lazy-imports
Lazy-ImportsThis is a Python tool to support lazy imports.
Likewise, the actual initialization of the module does not occur until usage time
to postponeModuleNotFoundErrors 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 ContentsMaintainersInstallationUsage & Example for LazyImporterUsage & Example for try_importSupport and FeedbackReporting Security VulnerabilitiesContributionCode of ConductLicensingMaintainersThis project is maintained by theOne Conversationteam ofDeutsche Telekom AG.
It is based on_LazyModulefromHuggingFaceandtry_import()from theOptuna framework.
Many thanks to HuggingFace foryour consentand to Optuna foryour consentto publish it as a standalone package 🤗 ♥.InstallationLazy-Imports is available atthe Python Package Index (PyPI).
It can be installed with pip:$pipinstalllazy-importsUsage & Example for LazyImporterA good and easy to understand example of how to use Lazy-Imports can be found in the__init__.pyfile of the HPOflow package.
It is printed here:importsysfromtypingimportTYPE_CHECKINGfromlazy_importsimportLazyImporterfromhpoflow.versionimport__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-checkingifTYPE_CHECKING:fromhpoflow.mlflowimport(# noqa: F401check_repo_is_dirty,normalize_mlflow_entry_name,normalize_mlflow_entry_names_in_dict,)fromhpoflow.optunaimportSignificanceRepeatedTrainingPruner# noqa: F401fromhpoflow.optuna_mlflowimportOptunaMLflow# noqa: F401fromhpoflow.optuna_transformersimportOptunaMLflowCallback# noqa: F401fromhpoflow.utilsimportfunc_no_exception_caller# noqa: F401else:sys.modules[__name__]=LazyImporter(__name__,globals()["__file__"],_import_structure,extra_objects={"__version__":__version__},)Usage & Example for try_importtry_importis 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:fromlazy_importsimporttry_importwithtry_import()asoptional_package_import:# use try_import as a context managerimportoptional_package# optional package that might not be installed# other non optional functions heredefoptional_function():# optional function that uses the optional packageoptional_package_import.check()# check if the import was ok or raise a meaningful exceptionoptional_package.some_external_function()# use the optional package hereSupport and FeedbackThe following channels are available for discussions, feedback, and support requests:open an issue in our GitHub repositorysend an e-mail to our open source teamReporting Security VulnerabilitiesThis project is built with security and data privacy in mind to ensure your data is safe.
We are grateful for security researchers and users reporting a vulnerability to us, first.
To ensure that your request is handled in a timely manner and non-disclosure of vulnerabilities
can be assured, please follow the below guideline.Please do not report security vulnerabilities directly on GitHub.
GitHub Issues can be publicly seen and therefore would result in a direct disclosure.Please address questions about data privacy, security concepts,
and other media requests to theopensource@telekom.demailbox.ContributionOur commitment to open source means that we are enabling - in fact encouraging - all interested
parties to contribute and become part of our developer community.Contribution and feedback is encouraged and always welcome. For more information about how to
contribute, as well as additional contribution information, see ourContribution Guidelines.Code of ConductThis project has adopted theContributor Covenantas our code of conduct. Please see the details in ourContributor Covenant Code of Conduct.
All contributors must abide by the code of conduct.LicensingCopyright (c) 2021Philip May,Deutsche Telekom AGCopyright (c) 2020, 2021The HuggingFace TeamCopyright (c) 2018 Preferred Networks, Inc.Licensed under theApache 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 athttp://www.apache.org/licenses/LICENSE-2.0Unless 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.