-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_venv.py
More file actions
94 lines (77 loc) · 3.03 KB
/
create_venv.py
File metadata and controls
94 lines (77 loc) · 3.03 KB
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
from handle_data import interpreters_data
import os
import subprocess
import sys
def _create_venv(interpreter_path, venv_dir, dry_run: bool = False):
abs_interpreter = os.path.abspath(interpreter_path)
abs_venv = os.path.abspath(venv_dir)
cmd = [abs_interpreter, "-m", "venv", abs_venv]
if dry_run:
stdout = f"DRY RUN: would run: {cmd}"
return True, stdout, ""
process = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True,
universal_newlines=True,
)
stdout, stderr = process.communicate()
return process.returncode == 0, stdout, stderr
def create_venv(interpreter_path, venv_dir, dry_run: bool = False):
if interpreters_data.interpreters is None:
print("Warning: no interpreters configured in data; proceeding with provided interpreter.")
success, stdout, stderr = _create_venv(interpreter_path, venv_dir, dry_run=dry_run)
if success:
if dry_run:
print(stdout)
return True
print(f"Virtual environment created successfully at {venv_dir}.")
return True
else:
print(f"Failed to create virtual environment. Error:\n{stderr}")
return False
def install_packages_in_venv(venv_dir: str, packages: list, dry_run: bool = False):
"""Install packages into the venv's Python using pip.
Each entry in `packages` may be either:
- a simple string: "numpy"
- a dict: {"packages": ["torch","torchvision"], "args": ["--index-url", "https://download.pytorch.org/whl/cu130"]}
Returns (success: bool, stdout: str, stderr: str).
"""
if sys.platform.startswith("win"):
py_exe = os.path.join(venv_dir, "Scripts", "python.exe")
else:
py_exe = os.path.join(venv_dir, "bin", "python")
all_stdout = []
all_stderr = []
for entry in packages:
if isinstance(entry, str):
pkg_list = [entry]
extra_args = []
elif isinstance(entry, dict):
pkg_list = entry.get("packages") or entry.get("package") or []
if isinstance(pkg_list, str):
pkg_list = [pkg_list]
extra_args = entry.get("args", []) or []
else:
# unsupported type; skip
continue
cmd = [os.path.abspath(py_exe), "-m", "pip", "install"] + pkg_list + extra_args
if dry_run:
all_stdout.append(f"DRY RUN: would run: {cmd}")
continue
if not os.path.exists(py_exe):
return False, "", f"Python executable not found in venv: {py_exe}"
process = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True,
universal_newlines=True,
)
stdout, stderr = process.communicate()
all_stdout.append(stdout)
all_stderr.append(stderr)
if process.returncode != 0:
return False, "\n".join(all_stdout), "\n".join(all_stderr)
return True, "\n".join(all_stdout), "\n".join(all_stderr)