speechbrain.utils.importutils moduleο
Module importing related utilities.
- Author
Sylvain de Langen 2024
Summaryο
Classes:
Defines a module type that lazily imports the target module using |
|
Defines a module type that lazily imports the target module, thus exposing contents without importing the target module needlessly. |
Functions:
Patches the module list to add a lazy redirection from |
|
Returns a list of importable scripts in the same module as the specified file. |
|
Makes |
|
Makes all modules under a module lazily importable merely by accessing them; e.g. |
Referenceο
- class speechbrain.utils.importutils.LazyModule(name: str, target: str, package: str | None)[source]ο
Bases:
ModuleTypeDefines a module type that lazily imports the target module, thus exposing contents without importing the target module needlessly.
- Parameters:
name (str) β Name of the module.
target (str) β Module to be loading lazily.
package (str, optional) β If specified, the target module load will be relative to this package. Depending on how you inject the lazy module into the environment, you may choose to specify the package here, or you may choose to include it into the
namewith the dot syntax. e.g. see howlazy_export()anddeprecated_redirect()differ.
- ensure_module(stacklevel: int) ModuleType[source]ο
Ensures that the target module is imported and available as
self.lazy_module, also returning it.- Parameters:
stacklevel (int) β The stack trace level of the function that caused the import to occur, relative to the caller of this function (e.g. if in function
fyou callensure_module(1), it will refer to the function that calledf).- Raises:
AttributeError β When the function responsible for the import attempt is found to be
inspect.py, we raise anAttributeErrorhere. This is because some code will inadvertently cause our modules to be imported, such as some of PyTorchβs op registering machinery.- Return type:
The target module after ensuring it is imported.
- class speechbrain.utils.importutils.DeprecatedModuleRedirect(old_import: str, new_import: str, extra_reason: str | None = None)[source]ο
Bases:
LazyModuleDefines a module type that lazily imports the target module using
LazyModule, but logging a deprecation warning when the import is actually being performed.This is only the module type itself; if you want to define a redirection, use
deprecated_redirect()instead.- Parameters:
old_import (str) β Old module import path e.g.
mypackage.myoldmodulenew_import (str) β New module import path e.g.
mypackage.mynewcoolmodule.mycoolsubmoduleextra_reason (str, optional) β If specified, extra text to attach to the warning for clarification (e.g. justifying why the move has occurred, or additional problems to look out for).
- ensure_module(stacklevel: int) ModuleType[source]ο
- speechbrain.utils.importutils.find_imports(file_path: str, find_subpackages: bool = False) List[str][source]ο
Returns a list of importable scripts in the same module as the specified file. e.g. if you have
foo/__init__.pyandfoo/bar.py, thenfiles_in_module("foo/__init__.py")then the result will be["bar"].Not recursive; this is only applies to the direct modules/subpackages of the package at the given path.
- Parameters:
- Returns:
imports β List of importable scripts with the same module.
- Return type:
List[str]
- speechbrain.utils.importutils.lazy_export(name: str, package: str)[source]ο
Makes
namelazily available under the module list for the specifiedpackage, unless it was loaded already, in which case it is ignored.
- speechbrain.utils.importutils.lazy_export_all(init_file_path: str, package: str, export_subpackages: bool = False)[source]ο
Makes all modules under a module lazily importable merely by accessing them; e.g.
foo/bar.pycould be accessed withfoo.bar.some_func().- Parameters:
init_file_path (str) β Path of the
__init__.pyfile, usually determined with__file__from there.package (str) β The relevant package, usually determined with
__name__from the__init__.py.export_subpackages (bool) β Whether we should make the subpackages (subdirectories) available directly as well.
- speechbrain.utils.importutils.deprecated_redirect(old_import: str, new_import: str, extra_reason: str | None = None, also_lazy_export: bool = False) None[source]ο
Patches the module list to add a lazy redirection from
old_importtonew_import, emitting aDeprecationWarningwhen imported.- Parameters:
old_import (str) β Old module import path e.g.
mypackage.myoldmodulenew_import (str) β New module import path e.g.
mypackage.mycoolpackage.mynewmoduleextra_reason (str, optional) β If specified, extra text to attach to the warning for clarification (e.g. justifying why the move has occurred, or additional problems to look out for).
also_lazy_export (bool) β Whether the module should also be exported as a lazy module in the package determined in
old_import. e.g. if you had afoo.bar.somefuncimport asold_import, assuming you havefooimported (or lazy loaded), you could usefoo.bar.somefuncdirectly without importingfoo.barexplicitly.