-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.py
31 lines (27 loc) · 901 Bytes
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import importlib
import os
def is_module_available(module_name: str):
"""
checks if a module is available or not (eg. _is_module_available("sentence_transformers"))
Args:
module_name (str): name of the model to check
Returns:
bool, if or not the given module exists
"""
try:
importlib.import_module(module_name)
except Exception as e:
# print(e)
return False
return True
def get_module_or_attr(module_name: str, func_name: str = None):
"""
Loads an attribute from a module or a module itself
(check if the module exists before calling this function)
"""
m = importlib.import_module(module_name)
if not func_name:
return m
if func_name not in dir(m):
raise ImportError(f"Cannot import {func_name} from {module_name}")
return getattr(m, func_name)