dynamic_imports
This module provides utilities for dynamic imports, allowing optional imports and importing modules from paths.
OptionalImports
dataclass
Handles optional imports, allowing modules to be imported only if they are available.
Attributes:
Name | Type | Description |
---|---|---|
imports |
dict[str, object]
|
A dictionary to store the imported modules. |
Example
Source code in lighter/utils/dynamic_imports.py
__getitem__(module_name)
Get the imported module by name, importing it if necessary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
module_name
|
str
|
Name of the module to import. |
required |
Raises:
Type | Description |
---|---|
ImportError
|
If the module is not available. |
Returns:
Name | Type | Description |
---|---|---|
object |
object
|
The imported module. |
Source code in lighter/utils/dynamic_imports.py
import_module_from_path(module_name, module_path)
Import a module from a given path and assign it a specified name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
module_name
|
str
|
Name to assign to the imported module. |
required |
module_path
|
str
|
Path to the module being imported. |
required |
Raises:
Type | Description |
---|---|
ValueError
|
If the module has already been imported. |
FileNotFoundError
|
If the |