-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstall_missing_packages.py
99 lines (88 loc) · 4.35 KB
/
install_missing_packages.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
90
91
92
93
94
95
96
97
98
99
import subprocess
import importlib
def is_mamba_installed():
try:
# Run the 'mamba --version' command
result = subprocess.run(['mamba', '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
# Check if the command was successful
if result.returncode == 0:
print("&&&& Mamba is installed.")
return True
else:
print("&&&& Mamba is not installed.")
return False
except FileNotFoundError:
# The command was not found
print("&&&& Mamba is not installed.")
return False
def install_with_mamba(packages):
print(f"&&&& Attempting to install {', '.join(packages)} with mamba.")
try:
# Run the 'mamba install <packages>' command
result = subprocess.run(['mamba', 'install', '-y'] + packages, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
# Check if the command was successful
if result.returncode == 0:
print(f"&&&& {', '.join(packages)} have been installed successfully with mamba.")
print(result.stdout)
else:
print(f"&&&& Failed to install {', '.join(packages)} with mamba.")
print(result.stderr)
except Exception as e:
print(f"&&&& An error occurred while trying to install {', '.join(packages)} with mamba: {e}")
def install_with_conda(packages):
print(f"&&&& Attempting to install {', '.join(packages)} with conda.")
try:
# Run the 'conda install <packages>' command
result = subprocess.run(['conda', 'install', '-y'] + packages, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
# Check if the command was successful
if result.returncode == 0:
print(f"&&&& {', '.join(packages)} have been installed successfully with conda.")
print(result.stdout)
else:
print(f"&&&& Failed to install {', '.join(packages)} with conda.")
print(result.stderr)
except Exception as e:
print(f"&&&& An error occurred while trying to install {', '.join(packages)} with conda: {e}")
def install_with_pip(packages):
print(f"&&&& Attempting to install {', '.join(packages)} with pip.")
try:
# Run the 'pip install <packages>' command
result = subprocess.run(['pip', 'install'] + packages, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
# Check if the command was successful
if result.returncode == 0:
print(f"&&&& {', '.join(packages)} have been installed successfully with pip.")
print(result.stdout)
else:
print(f"&&&& Failed to install {', '.join(packages)} with pip.")
print(result.stderr)
except Exception as e:
print(f"&&&& An error occurred while trying to install {', '.join(packages)} with pip: {e}")
def live_package_installation():
packages_to_install = ['hnswlib', 'parc', 'sklearn_ann', 'annoy', 'pyNNDescent'] # last two probably only needed for published dashboards
installers_to_use = ['mamba', 'conda', 'pip']
for package in packages_to_install:
try:
importlib.import_module(package.lower())
print(f"&&&& {package} is already installed.")
except ImportError:
print(f"&&&& {package} is not installed.")
for installer in installers_to_use:
print(f"&&&& Trying to install {package} using {installer}.")
if installer == 'mamba':
if is_mamba_installed():
install_with_mamba([package])
else:
print(f"&&&& mamba is not installed. Trying the next installer.")
continue
elif installer == 'conda':
install_with_conda([package])
elif installer == 'pip':
install_with_pip([package])
try:
importlib.import_module(package.lower())
print(f"&&&& {package} has been successfully installed using {installer}.")
break
except ImportError:
print(f"&&&& {package} was not successfully installed with {installer}.")
else:
print(f"&&&& {package} could not be installed after trying all installers.")