-
Notifications
You must be signed in to change notification settings - Fork 1
/
dependencies.py
64 lines (47 loc) · 1.55 KB
/
dependencies.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""
Wrapper on Pip to install required python modules
"""
import subprocess
import importlib
def check_module(module, min_version=""):
try:
m = importlib.import_module(module)
if min_version and not check_minimum_version(m.__version__, min_version):
return False
except ModuleNotFoundError:
return False
return True
def check_pip():
return check_module("pip")
def check_minimum_version(module_version, min_version):
try:
from packaging import version
return version.parse(module_version) >= version.parse(min_version)
except ModuleNotFoundError:
return module_version >= min_version
def install(dep, upgrade=False):
module = None
module_version = ""
if not check_pip():
return module, module_version
cmd = ["python3", "-m", "pip", "install", dep, "--user"]
if upgrade:
cmd.append("--upgrade")
# Prevents the call to pip from spawning an console on Windows.
try:
subprocess.run(cmd, check=False, creationflags=subprocess.CREATE_NO_WINDOW)
except (AttributeError, TypeError):
subprocess.run(cmd, check=False)
# Even if process failed, try to import module
try:
module = importlib.import_module(dep)
module_version = module.__version__
except ModuleNotFoundError:
pass
return module, module_version
def import_or_install(dep):
try:
module = importlib.import_module(dep)
return module, module.__version__
except ModuleNotFoundError:
return install(dep)