-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimports.py
More file actions
23 lines (21 loc) · 913 Bytes
/
imports.py
File metadata and controls
23 lines (21 loc) · 913 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from importlib import import_module
def import_optional_dependency(module_to_import: str, dependency: str = None):
"""
Attempt to import a module from an external dependency.
Raise an error if that dependency is not installed.
Args:
module_to_import: A string containing the module to be imported
dependency: Information about the dependency required
Raises:
ModuleNotFoundError: If the dependency is not installed
Examples:
>>> math = import_optional_dependency('math')
>>> pyyaml = import_optional_dependency("pyyaml")
Traceback (most recent call last):
...
ModuleNotFoundError: pyyaml is required to use this function.
"""
try:
return import_module(module_to_import)
except ModuleNotFoundError:
raise ModuleNotFoundError(f"{dependency or module_to_import} is required to use this function.")