Delete main/src/optional_import copy.py with huggingface_hub
Browse files
main/src/optional_import copy.py
DELETED
|
@@ -1,38 +0,0 @@
|
|
| 1 |
-
import importlib
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
class _LazyRaise:
|
| 5 |
-
def __init__(self, msg):
|
| 6 |
-
self.msg = msg
|
| 7 |
-
|
| 8 |
-
def __getattr__(self, name):
|
| 9 |
-
raise RuntimeError(self.msg)
|
| 10 |
-
|
| 11 |
-
def __call__(self, *args, **kwargs):
|
| 12 |
-
raise RuntimeError(self.msg)
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
def optional_import(module_name: str, additional_error_message=''):
|
| 16 |
-
"""
|
| 17 |
-
Optional module import.
|
| 18 |
-
|
| 19 |
-
Raise an error only when a module is being used
|
| 20 |
-
|
| 21 |
-
Args:
|
| 22 |
-
module_name: the name of the module to import
|
| 23 |
-
additional_error_message: add a custom error message
|
| 24 |
-
|
| 25 |
-
Returns:
|
| 26 |
-
the module
|
| 27 |
-
|
| 28 |
-
Examples:
|
| 29 |
-
>>> nn = optional_import('torch.nn')
|
| 30 |
-
>>> print(nn.ReLU)
|
| 31 |
-
"""
|
| 32 |
-
try:
|
| 33 |
-
m = importlib.import_module(module_name)
|
| 34 |
-
if m is not None:
|
| 35 |
-
return m
|
| 36 |
-
except:
|
| 37 |
-
return _LazyRaise(f'optional module cannot be imported `{module_name}`. '
|
| 38 |
-
f'To use this functionality, this module must be installed! {additional_error_message}')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|