-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup_env.py
57 lines (51 loc) · 1.92 KB
/
setup_env.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 yaml
def create_default_yml(env_name):
"""Creates an environment.yml file with the environment setup if it doesn't exist."""
if not os.path.exists("environment.yml"):
environment_data = {
'name': env_name,
'channels': ['defaults'],
'dependencies': [
'python=3.10',
'pip',
'pyyaml'
]
}
with open("environment.yml", 'w') as file:
yaml.dump(environment_data, file, default_flow_style=False)
print("Environment YAML file created with local settings.")
else:
print("Environment YAML file already exists.")
def create_env_directly(env_name):
"""Create the Conda environment directly using conda create without specifying a prefix."""
print(f"Attempting to create Conda environment '{env_name}' in the default Conda location")
result = subprocess.run(
["conda", "create", "--name", env_name, "-y", "python=3.10", "pip", "pyyaml"],
capture_output=True, text=True
)
if result.returncode == 0:
print("Local environment created successfully.")
print(result.stdout)
else:
print("Failed to create environment.")
print(result.stderr)
def check_env_exists(env_name):
"""Check if the Conda environment exists."""
result = subprocess.run(
["conda", "env", "list"],
capture_output=True, text=True
)
return env_name in result.stdout
if __name__ == "__main__":
project_directory = os.getcwd()
folder_name = os.path.basename(project_directory)
env_name = folder_name + "_env"
# Create the environment.yml file if it doesn't exist
create_default_yml(env_name)
# Check if environment exists, create if not
if not check_env_exists(env_name):
create_env_directly(env_name)
else:
print(f"Conda environment '{env_name}' already exists.")