-
Notifications
You must be signed in to change notification settings - Fork 329
/
install.py
89 lines (70 loc) · 2.09 KB
/
install.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
from __future__ import annotations
import importlib.util
import subprocess
import sys
from importlib.metadata import version # python >= 3.8
from packaging.version import parse
import_name = {"py-cpuinfo": "cpuinfo", "protobuf": "google.protobuf"}
custom_requirements = {
"ultralytics": "ultralytics>=8.3.0,!=8.3.41,!=8.3.42,!=8.3.45,!=8.3.46"
}
excluded_versions = {"ultralytics": ("8.3.41", "8.3.42", "8.3.45", "8.3.46")}
def is_installed(
package: str,
min_version: str | None = None,
max_version: str | None = None,
):
name = import_name.get(package, package)
excluded = excluded_versions.get(package, ())
try:
spec = importlib.util.find_spec(name)
except ModuleNotFoundError:
return False
if spec is None:
return False
if not min_version and not max_version:
return True
if not min_version:
min_version = "0.0.0"
if not max_version:
max_version = "99999999.99999999.99999999"
try:
pkg_version = version(package)
return (
parse(min_version) <= parse(pkg_version) <= parse(max_version)
and pkg_version not in excluded
)
except Exception:
return False
def run_pip(*args):
subprocess.run([sys.executable, "-m", "pip", "install", *args], check=True)
def install():
deps = [
# requirements
("ultralytics", "8.3.0", None),
("mediapipe", "0.10.13", "0.10.15"),
("rich", "13.0.0", None),
]
pkgs = []
for pkg, low, high in deps:
if not is_installed(pkg, low, high):
if pkg in custom_requirements:
cmd = custom_requirements[pkg]
elif low and high:
cmd = f"{pkg}>={low},<={high}"
elif low:
cmd = f"{pkg}>={low}"
elif high:
cmd = f"{pkg}<={high}"
else:
cmd = pkg
pkgs.append(cmd)
if pkgs:
run_pip(*pkgs)
try:
import launch
skip_install = launch.args.skip_install
except Exception:
skip_install = False
if not skip_install:
install()