-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsetup_correct_python.py
More file actions
329 lines (289 loc) · 11 KB
/
Copy pathsetup_correct_python.py
File metadata and controls
329 lines (289 loc) · 11 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
"""
Setup script that enforces Python 3.11 or 3.12
===============================================
This script will find and use Python 3.11 or 3.12 to create the virtual environment.
"""
import sys
import subprocess
import os
import shutil
import logging
from pathlib import Path
from typing import Optional, Tuple, List
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
def find_python_311_or_312() -> Optional[Tuple[str, str]]:
"""
Find Python 3.11 or 3.12 executable on the system.
Returns:
Tuple of (version, executable_path) if found, None otherwise
"""
candidates: List[Tuple[str, str]] = []
# Windows: Check py launcher
if sys.platform == "win32":
for version in ["3.12", "3.11"]:
try:
result = subprocess.run(
["py", f"-{version}", "--version"],
capture_output=True,
text=True,
timeout=5,
check=False
)
if result.returncode == 0:
# Get full path
result2 = subprocess.run(
["py", f"-{version}", "-c", "import sys; print(sys.executable)"],
capture_output=True,
text=True,
timeout=5,
check=False
)
if result2.returncode == 0:
exe = result2.stdout.strip()
if exe and Path(exe).exists():
candidates.append((version, exe))
logger.debug(f"Found Python {version} via py launcher: {exe}")
except (subprocess.TimeoutExpired, FileNotFoundError, OSError) as e:
logger.debug(f"Could not check Python {version} via py launcher: {e}")
continue
# Check common installation paths
common_paths: List[str] = []
if sys.platform == "win32":
username = os.getenv("USERNAME", "")
common_paths = [
r"C:\Python312\python.exe",
r"C:\Python311\python.exe",
r"C:\Program Files\Python312\python.exe",
r"C:\Program Files\Python311\python.exe",
rf"C:\Users\{username}\AppData\Local\Programs\Python\Python312\python.exe",
rf"C:\Users\{username}\AppData\Local\Programs\Python\Python311\python.exe",
]
else:
common_paths = [
"/usr/bin/python3.12",
"/usr/bin/python3.11",
"/usr/local/bin/python3.12",
"/usr/local/bin/python3.11",
"/opt/homebrew/bin/python3.12",
"/opt/homebrew/bin/python3.11",
]
for path_str in common_paths:
path = Path(path_str)
if path.exists():
try:
result = subprocess.run(
[str(path), "--version"],
capture_output=True,
text=True,
timeout=5,
check=False
)
if result.returncode == 0:
version_str = result.stdout.strip()
if "3.12" in version_str:
candidates.append(("3.12", str(path)))
logger.debug(f"Found Python 3.12 at: {path}")
elif "3.11" in version_str:
candidates.append(("3.11", str(path)))
logger.debug(f"Found Python 3.11 at: {path}")
except (subprocess.TimeoutExpired, FileNotFoundError, OSError) as e:
logger.debug(f"Could not verify Python at {path}: {e}")
continue
# Prefer 3.12, then 3.11
if candidates:
candidates.sort(key=lambda x: x[0], reverse=True)
logger.info(f"Found {len(candidates)} Python installation(s)")
return candidates[0]
logger.warning("No compatible Python version found")
return None
def verify_python_version(python_exe: str) -> bool:
"""
Verify that the Python executable is version 3.11 or 3.12.
Args:
python_exe: Path to Python executable
Returns:
True if version is valid, False otherwise
"""
try:
result = subprocess.run(
[python_exe, "-c", "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')"],
capture_output=True,
text=True,
timeout=5,
check=False
)
if result.returncode == 0:
version_str = result.stdout.strip()
try:
major, minor = map(int, version_str.split('.'))
return major == 3 and minor in (11, 12)
except ValueError:
return False
return False
except Exception as e:
logger.error(f"Failed to verify Python version: {e}")
return False
def create_virtual_environment(python_exe: str, venv_path: Path) -> bool:
"""
Create virtual environment using specified Python executable.
Args:
python_exe: Path to Python executable
venv_path: Path where venv should be created
Returns:
True if successful, False otherwise
"""
if venv_path.exists():
logger.info(f"Removing existing virtual environment at {venv_path}")
try:
shutil.rmtree(venv_path)
except OSError as e:
logger.error(f"Failed to remove existing venv: {e}")
return False
try:
logger.info(f"Creating virtual environment with {python_exe}")
result = subprocess.run(
[python_exe, "-m", "venv", str(venv_path)],
check=True,
capture_output=True,
text=True,
timeout=60
)
logger.info("Virtual environment created successfully")
return True
except subprocess.CalledProcessError as e:
logger.error(f"Failed to create venv: {e}")
if e.stderr:
logger.error(f"Error output: {e.stderr}")
return False
except subprocess.TimeoutExpired:
logger.error("Virtual environment creation timed out")
return False
def install_dependencies(pip_exe: Path) -> bool:
"""
Install project dependencies using pip.
Args:
pip_exe: Path to pip executable
Returns:
True if successful, False otherwise
"""
requirements_file = Path("requirements.txt")
if not requirements_file.exists():
logger.error("requirements.txt not found")
return False
# Upgrade pip first
logger.info("Upgrading pip...")
try:
subprocess.run(
[str(pip_exe), "install", "--upgrade", "pip"],
check=True,
timeout=300,
capture_output=True
)
except (subprocess.CalledProcessError, subprocess.TimeoutExpired) as e:
logger.warning(f"Failed to upgrade pip (non-critical): {e}")
# Install requirements
logger.info("Installing dependencies (this may take several minutes)...")
try:
result = subprocess.run(
[str(pip_exe), "install", "-r", "requirements.txt"],
check=True,
timeout=1800, # 30 minutes max
capture_output=True,
text=True
)
logger.info("Dependencies installed successfully")
return True
except subprocess.CalledProcessError as e:
logger.error(f"Failed to install dependencies: {e}")
if e.stderr:
logger.error(f"Error output: {e.stderr[:500]}") # Limit output
return False
except subprocess.TimeoutExpired:
logger.error("Dependency installation timed out")
return False
def main() -> None:
"""Main setup function"""
print("=" * 70)
print("Enterprise LangChain AI Workbench - Python Version Setup")
print("=" * 70)
print()
# Check current Python version
current_version = f"{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}"
print(f"Current Python: {current_version}")
if sys.version_info.minor >= 13:
print("ERROR: Python 3.13 is NOT compatible with LangChain/Pydantic")
print(" You MUST use Python 3.11 or 3.12")
print()
print("Searching for Python 3.11 or 3.12...")
python_info = find_python_311_or_312()
if not python_info:
print()
print("=" * 70)
print("ERROR: Python 3.11 or 3.12 not found!")
print("=" * 70)
print()
print("Please install Python 3.12:")
print(" Windows: https://www.python.org/downloads/release/python-3120/")
print(" macOS: brew install python@3.12")
print(" Linux: sudo apt install python3.12 python3.12-venv")
print()
print("After installing, run this script again.")
sys.exit(1)
version, python_exe = python_info
# Verify the found Python version
if not verify_python_version(python_exe):
print(f"ERROR: Found Python at {python_exe} but version verification failed")
sys.exit(1)
print(f"Found Python {version} at: {python_exe}")
print()
print(f"Creating virtual environment with Python {version}...")
venv_path = Path("venv")
if not create_virtual_environment(python_exe, venv_path):
print("ERROR: Failed to create virtual environment")
sys.exit(1)
# Get pip executable
if sys.platform == "win32":
pip_exe = venv_path / "Scripts" / "pip.exe"
else:
pip_exe = venv_path / "bin" / "pip"
if not pip_exe.exists():
print(f"ERROR: pip executable not found at {pip_exe}")
sys.exit(1)
# Install dependencies
if not install_dependencies(pip_exe):
print("ERROR: Failed to install dependencies")
sys.exit(1)
print()
print("=" * 70)
print("Setup Complete!")
print("=" * 70)
print()
print("To activate the virtual environment:")
if sys.platform == "win32":
print(" venv\\Scripts\\activate")
print(" Or: venv\\Scripts\\Activate.ps1")
else:
print(" source venv/bin/activate")
print()
print("Then run: python start_app.py")
print("=" * 70)
elif sys.version_info.minor < 11:
print("ERROR: Python 3.11 or higher is required")
print(f" Current version: {current_version}")
sys.exit(1)
else:
print("Python version OK - proceeding with setup...")
# Run normal setup if available
try:
import setup
setup.main()
except ImportError:
logger.warning("setup.py not found, skipping normal setup")
print("Note: setup.py not found. Using current Python environment.")
if __name__ == "__main__":
main()