-
Notifications
You must be signed in to change notification settings - Fork 0
/
req.py
57 lines (50 loc) · 2.08 KB
/
req.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
import os
import subprocess
import sys
import yaml
def read_env_name_from_yaml(yaml_file):
"""Reads the environment name from a YAML file."""
if not os.path.exists(yaml_file):
print(f"{yaml_file} not found. Exiting...")
exit(1)
with open(yaml_file, 'r') as file:
data = yaml.safe_load(file)
return data.get('name', None)
def check_environment_exists(env_name):
"""Checks if the specified Conda environment exists."""
result = subprocess.run(["conda", "env", "list"], capture_output=True, text=True)
if env_name in result.stdout:
return True
return False
def is_running_in_env(env_name):
"""Checks if the current Python executable is running within the specified environment."""
python_exec_path = sys.executable
expected_path_segment = os.path.join("envs", env_name)
if expected_path_segment in python_exec_path:
return True
return False
def run_pip_install(env_name):
"""Runs pip install from requirements.txt within a specified Conda environment."""
if not os.path.exists("requirements.txt"):
print("requirements.txt not found. Exiting...")
exit(1)
if not is_running_in_env(env_name):
print(f"Not running in the {env_name} environment. Please activate the environment and try again.")
exit(1)
print("Found requirements.txt. Installing dependencies...")
command = f"conda run -n {env_name} pip install -r requirements.txt --upgrade"
result = subprocess.run(command, shell=True, capture_output=True, text=True)
if result.returncode == 0:
print("Dependencies installed/updated successfully.")
print(result.stdout)
else:
print("Failed to install/update dependencies.")
print(result.stderr)
# Main script starts here
if __name__ == "__main__":
yaml_file = 'environment.yml'
env_name = read_env_name_from_yaml(yaml_file)
if env_name and check_environment_exists(env_name):
run_pip_install(env_name)
else:
print(f"Environment {env_name} does not exist or the name could not be determined. Exiting...")