fix host.py venv creation and pip installation
Hunter
Wednesday, February 4th, 2026 at 10:04 PM
20
21 def setup_venv():
22 """Create and setup virtual environment if it doesn't exist"""
- if not VENV_DIR.exists():
- print("Creating virtual environment...")
23 + # Determine the path to pip and python in the venv
24 + if sys.platform == "win32":
25 + pip_path = VENV_DIR / "Scripts" / "pip"
26 + python_path = VENV_DIR / "Scripts" / "python"
27 + else:
28 + pip_path = VENV_DIR / "bin" / "pip"
29 + python_path = VENV_DIR / "bin" / "python3"
30 +
31 + # Check if venv needs to be created or recreated
32 + if not VENV_DIR.exists() or not python_path.exists():
33 + if VENV_DIR.exists():
34 + print("Virtual environment incomplete, recreating...")
35 + import shutil
36 + shutil.rmtree(VENV_DIR)
37 + else:
38 + print("Creating virtual environment...")
39 +
40 try:
41 subprocess.check_call([sys.executable, "-m", "venv", str(VENV_DIR)])
42 print("Virtual environment created successfully.")
44 print(f"Error creating virtual environment: {e}")
45 sys.exit(1)
46
- # Determine the path to pip in the venv
- if sys.platform == "win32":
- pip_path = VENV_DIR / "Scripts" / "pip"
- python_path = VENV_DIR / "Scripts" / "python"
- else:
- pip_path = VENV_DIR / "bin" / "pip"
- python_path = VENV_DIR / "bin" / "python3"
47 + # Ensure pip is available (sometimes venv doesn't include it)
48 + if not pip_path.exists():
49 + print("Installing pip in virtual environment...")
50 + try:
51 + subprocess.check_call([str(python_path), "-m", "ensurepip", "--upgrade"])
52 + except subprocess.CalledProcessError as e:
53 + print(f"Error ensuring pip: {e}")
54 + sys.exit(1)
55
56 # Install requirements if requirements.txt exists
57 if REQUIREMENTS_FILE.exists():
58 print("Installing dependencies from requirements.txt...")
59 try:
- subprocess.check_call([str(pip_path), "install", "-q", "-r", str(REQUIREMENTS_FILE)])
60 + subprocess.check_call([str(python_path), "-m", "pip", "install", "-q", "-r", str(REQUIREMENTS_FILE)])
61 print("Dependencies installed successfully.\n")
62 except subprocess.CalledProcessError as e:
63 print(f"Error installing dependencies: {e}")