-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodules.py
More file actions
37 lines (28 loc) · 724 Bytes
/
modules.py
File metadata and controls
37 lines (28 loc) · 724 Bytes
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
32
33
34
35
36
37
#!/usr/bin/env python
import subprocess
import sys
import typing as ty
def install(package: str, quiet: bool = False, upgrade: bool = False):
cmd = [sys.executable, "-m", "pip", "install"]
if upgrade:
cmd += ["--upgrade"]
if quiet:
cmd += ["-q"]
cmd += [package]
subprocess.check_call(cmd)
def import_module(module: str, base_package: str = None) -> ty.Any:
try:
import importlib
except ImportError:
install('importlib')
import importlib
try:
return importlib.import_module(module)
except ImportError:
if base_package is None:
install(module)
else:
install(base_package)
return importlib.import_module(module)
if __name__ == "__main__":
pass