-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpython-environment-manager.ps1
More file actions
587 lines (487 loc) · 19.8 KB
/
Copy pathpython-environment-manager.ps1
File metadata and controls
587 lines (487 loc) · 19.8 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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
<#
.SYNOPSIS
Comprehensive Python Environment Manager for MLCreator
Ensures all Python systems have proper configuration and dependencies
.DESCRIPTION
This script provides complete Python environment management including:
- Version verification and setup
- Virtual environment management
- Dependency installation and verification
- Configuration file updates
- Health checks and diagnostics
.NOTES
Author: MLcreator AI Assistant
Date: 2025-11-20
Requires: PowerShell 5.1+, Python 3.8+
#>
param(
[switch]$Setup, # Full environment setup
[switch]$Verify, # Verify current setup
[switch]$Update, # Update dependencies
[switch]$Clean, # Clean and rebuild environment
[switch]$Diagnose, # Run diagnostics
[switch]$Force # Force operations without prompts
)
$ErrorActionPreference = "Stop"
# Configuration
$PythonVersion = "3.13.5"
$ProjectRoot = $PSScriptRoot
$RequirementsFile = Join-Path $ProjectRoot "requirements.txt"
$VenvPath = Join-Path $ProjectRoot "serena-env"
$PyrightConfigFile = Join-Path $ProjectRoot "pyrightconfig.json"
$VscodeSettingsFile = Join-Path $ProjectRoot ".vscode\settings.json"
Write-Host "🐍 MLCreator Python Environment Manager" -ForegroundColor Cyan
Write-Host "======================================" -ForegroundColor Cyan
Write-Host ""
#region Helper Functions
function Test-Command {
param([string]$Command)
try {
$null = Get-Command $Command -ErrorAction Stop
return $true
} catch {
return $false
}
}
function Get-PythonVersion {
try {
$version = & python --version 2>&1
if ($version -match 'Python (\d+)\.(\d+)\.(\d+)') {
return @{
Full = $version
Major = [int]$matches[1]
Minor = [int]$matches[2]
Patch = [int]$matches[3]
Numeric = ([int]$matches[1] * 1000000 + [int]$matches[2] * 1000 + [int]$matches[3])
}
}
} catch {
return $null
}
return $null
}
function Test-VirtualEnvironment {
$pythonExe = Join-Path $VenvPath "Scripts\python.exe"
return Test-Path $pythonExe
}
function Invoke-VenvCommand {
param([string]$Command, [switch]$Silent)
$pythonExe = Join-Path $VenvPath "Scripts\python.exe"
if (-not (Test-Path $pythonExe)) {
throw "Virtual environment Python executable not found: $pythonExe"
}
if (-not $Silent) {
Write-Host " Running: $Command" -ForegroundColor Gray
}
# Use Start-Process for better argument handling
$process = Start-Process -FilePath $pythonExe -ArgumentList $Command -NoNewWindow -Wait -PassThru -RedirectStandardOutput "$env:TEMP\python_stdout.txt" -RedirectStandardError "$env:TEMP\python_stderr.txt"
$stdout = Get-Content "$env:TEMP\python_stdout.txt" -ErrorAction SilentlyContinue
$stderr = Get-Content "$env:TEMP\python_stderr.txt" -ErrorAction SilentlyContinue
# Clean up temp files
Remove-Item "$env:TEMP\python_stdout.txt" -ErrorAction SilentlyContinue
Remove-Item "$env:TEMP\python_stderr.txt" -ErrorAction SilentlyContinue
if ($process.ExitCode -ne 0) {
$errorMsg = if ($stderr) { $stderr } else { "Command failed with exit code $($process.ExitCode)" }
throw $errorMsg
}
return $stdout
}
#endregion
#region Core Functions
function Invoke-Setup {
Write-Host "`n🔧 Setting up complete Python environment..." -ForegroundColor Yellow
# 1. Verify Python version
Write-Host "`n1. Checking Python version..." -ForegroundColor Cyan
$pythonInfo = Get-PythonVersion
if (-not $pythonInfo) {
throw "Python not found in PATH. Please install Python $PythonVersion or higher."
}
Write-Host " ✓ Python found: $($pythonInfo.Full)" -ForegroundColor Green
if ($pythonInfo.Numeric -lt 3008000) {
Write-Host " ⚠️ Python version is below recommended 3.8. Consider upgrading." -ForegroundColor Yellow
}
# 2. Setup virtual environment
Write-Host "`n2. Setting up virtual environment..." -ForegroundColor Cyan
if ((Test-Path $VenvPath) -and -not $Force) {
Write-Host " ✓ Virtual environment already exists" -ForegroundColor Green
} else {
if (Test-Path $VenvPath) {
Write-Host " 🗑️ Removing existing virtual environment..." -ForegroundColor Yellow
Remove-Item $VenvPath -Recurse -Force
}
Write-Host " 📦 Creating virtual environment..." -ForegroundColor Gray
& python -m venv $VenvPath
if (-not (Test-VirtualEnvironment)) {
throw "Failed to create virtual environment"
}
Write-Host " ✓ Virtual environment created" -ForegroundColor Green
}
# 3. Upgrade pip
Write-Host "`n3. Upgrading pip..." -ForegroundColor Cyan
try {
Invoke-VenvCommand "-m pip install --upgrade pip" -Silent
Write-Host " ✓ Pip upgraded" -ForegroundColor Green
} catch {
Write-Host " ⚠️ Pip upgrade failed, continuing..." -ForegroundColor Yellow
}
# 4. Install dependencies
Write-Host "`n4. Installing dependencies..." -ForegroundColor Cyan
if (-not (Test-Path $RequirementsFile)) {
throw "Requirements file not found: $RequirementsFile"
}
Invoke-VenvCommand "-m pip install -r $RequirementsFile" -Silent
Write-Host " ✓ Dependencies installed" -ForegroundColor Green
# 5. Setup NLTK data
Write-Host "`n5. Setting up NLTK data..." -ForegroundColor Cyan
$nltkScript = @"
import nltk
import ssl
import sys
try:
_create_unverified_https_context = ssl._create_unverified_context
except AttributeError:
pass
else:
ssl._create_default_https_context = _create_unverified_https_context
packages = ['punkt', 'stopwords', 'wordnet', 'averaged_perceptron_tagger']
for package in packages:
try:
nltk.download(package, quiet=True)
print(f"✓ Downloaded: {package}")
except Exception as e:
print(f"⚠️ Failed {package}: {e}")
print("NLTK setup complete")
"@
$tempScript = "$env:TEMP\nltk_setup.py"
$nltkScript | Out-File -FilePath $tempScript -Encoding UTF8
try {
Invoke-VenvCommand $tempScript -Silent
Write-Host " ✓ NLTK data downloaded" -ForegroundColor Green
} catch {
Write-Host " ⚠️ NLTK setup had issues, but tools will work with reduced functionality" -ForegroundColor Yellow
} finally {
Remove-Item $tempScript -Force -ErrorAction SilentlyContinue
}
# 6. Update configuration files
Write-Host "`n6. Updating configuration files..." -ForegroundColor Cyan
# Update pyrightconfig.json
if (Test-Path $PyrightConfigFile) {
$config = Get-Content $PyrightConfigFile -Raw | ConvertFrom-Json
$config.pythonVersion = "3.13"
$config.executionEnvironments[0].pythonVersion = "3.13"
$config | ConvertTo-Json -Depth 10 | Set-Content $PyrightConfigFile -Encoding UTF8
Write-Host " ✓ Updated pyrightconfig.json" -ForegroundColor Green
}
# Update VS Code settings
$vscodeDir = Split-Path $VscodeSettingsFile -Parent
if (-not (Test-Path $vscodeDir)) {
New-Item -ItemType Directory -Path $vscodeDir | Out-Null
}
if (Test-Path $VscodeSettingsFile) {
$settings = Get-Content $VscodeSettingsFile -Raw | ConvertFrom-Json
} else {
$settings = @{}
}
$settings."python.defaultInterpreterPath" = Join-Path $VenvPath "Scripts\python.exe"
$settings."python.terminal.activateEnvironment" = $true
$settings."python.venvPath" = $VenvPath
$settings | ConvertTo-Json -Depth 10 | Set-Content $VscodeSettingsFile -Encoding UTF8
Write-Host " ✓ Updated VS Code settings" -ForegroundColor Green
# 7. Update .python-version
$PythonVersion | Set-Content (Join-Path $ProjectRoot ".python-version") -Encoding UTF8
Write-Host " ✓ Updated .python-version" -ForegroundColor Green
Write-Host "`n✅ Python environment setup complete!" -ForegroundColor Green
}
function Invoke-Verify {
Write-Host "`n🔍 Verifying Python environment..." -ForegroundColor Yellow
$issues = @()
$warnings = @()
# 1. Check Python version
Write-Host "`n1. Checking Python version..." -ForegroundColor Cyan
$pythonInfo = Get-PythonVersion
if (-not $pythonInfo) {
$issues += "Python not found in PATH"
} else {
Write-Host " ✓ Python: $($pythonInfo.Full)" -ForegroundColor Green
if ($pythonInfo.Numeric -lt 3008000) {
$warnings += "Python version below 3.8 (found $($pythonInfo.Full))"
}
}
# 2. Check virtual environment
Write-Host "`n2. Checking virtual environment..." -ForegroundColor Cyan
if (-not (Test-VirtualEnvironment)) {
$issues += "Virtual environment not found at: $VenvPath"
} else {
Write-Host " ✓ Virtual environment exists" -ForegroundColor Green
# Test virtual environment Python
try {
$venvVersion = Invoke-VenvCommand "--version" -Silent
Write-Host " ✓ Virtual environment Python: $venvVersion" -ForegroundColor Green
} catch {
$issues += "Virtual environment Python is not working: $_"
}
}
# 3. Check dependencies
Write-Host "`n3. Checking dependencies..." -ForegroundColor Cyan
if (Test-VirtualEnvironment) {
try {
$installedJson = Invoke-VenvCommand "-m pip list --format=json" -Silent
if ($installedJson) {
$installed = $installedJson | ConvertFrom-Json
} else {
$installed = @()
}
$required = @(
"nltk",
"numpy",
"pandas",
"matplotlib",
"PyYAML",
"requests"
)
foreach ($package in $required) {
$found = $installed | Where-Object { $_.name -eq $package }
if ($found) {
Write-Host " ✓ $package $($found.version)" -ForegroundColor Green
} else {
$issues += "Required package not found: $package"
}
}
# Check optional packages
$optional = @(
"sentence-transformers",
"torch",
"transformers",
"scikit-learn",
"scipy"
)
foreach ($package in $optional) {
$found = $installed | Where-Object { $_.name -eq $package }
if ($found) {
Write-Host " ✓ $package $($found.version) (optional)" -ForegroundColor Green
} else {
$warnings += "Optional package not found: $package"
}
}
} catch {
$issues += "Failed to check installed packages: $_"
}
}
# 4. Check configuration files
Write-Host "`n4. Checking configuration files..." -ForegroundColor Cyan
# Check pyrightconfig.json
if (Test-Path $PyrightConfigFile) {
try {
$config = Get-Content $PyrightConfigFile -Raw | ConvertFrom-Json
if ($config.pythonVersion -eq "3.13") {
Write-Host " ✓ pyrightconfig.json Python version: 3.13" -ForegroundColor Green
} else {
$warnings += "pyrightconfig.json has outdated Python version: $($config.pythonVersion)"
}
} catch {
$issues += "Invalid pyrightconfig.json: $_"
}
} else {
$issues += "pyrightconfig.json not found"
}
# Check VS Code settings
if (Test-Path $VscodeSettingsFile) {
try {
$settingsContent = Get-Content $VscodeSettingsFile -Raw
# Check if the file contains the expected Python interpreter path
$expectedPath = Join-Path $VenvPath "Scripts\python.exe"
$expectedPathEscaped = [regex]::Escape($expectedPath)
if ($settingsContent -match $expectedPathEscaped) {
Write-Host " ✓ VS Code Python interpreter path configured" -ForegroundColor Green
} else {
$warnings += "VS Code Python interpreter path may be incorrect"
}
} catch {
$issues += "Error checking VS Code settings: $_"
}
} else {
$warnings += "VS Code settings not found"
}
# 5. Test imports
Write-Host "`n5. Testing critical imports..." -ForegroundColor Cyan
if (Test-VirtualEnvironment) {
$tests = @(
@{Name="NLTK"; Command="import nltk; print('OK')"},
@{Name="NumPy"; Command="import numpy as np; print('OK')"},
@{Name="Pandas"; Command="import pandas as pd; print('OK')"},
@{Name="PyYAML"; Command="import yaml; print('OK')"},
@{Name="Requests"; Command="import requests; print('OK')"}
)
foreach ($test in $tests) {
try {
$result = Invoke-VenvCommand "-c `"$($test.Command)`"" -Silent
if ($result -and $result -match "OK") {
Write-Host " ✓ $($test.Name) import works" -ForegroundColor Green
} else {
$issues += "$($test.Name) import test failed (no OK response)"
}
} catch {
$issues += "$($test.Name) import failed: $($_.Exception.Message)"
}
}
}
# Report results
Write-Host "`n📊 Verification Results:" -ForegroundColor Cyan
if ($issues.Count -eq 0) {
Write-Host "`n✅ All critical checks passed!" -ForegroundColor Green
} else {
Write-Host "`n❌ Issues found:" -ForegroundColor Red
foreach ($issue in $issues) {
Write-Host " • $issue" -ForegroundColor Red
}
}
if ($warnings.Count -gt 0) {
Write-Host "`n⚠️ Warnings:" -ForegroundColor Yellow
foreach ($warning in $warnings) {
Write-Host " • $warning" -ForegroundColor Yellow
}
}
return $issues.Count -eq 0
}
function Invoke-Update {
Write-Host "`n⬆️ Updating Python environment..." -ForegroundColor Yellow
if (-not (Test-VirtualEnvironment)) {
Write-Host " ⚠️ Virtual environment not found. Running setup first..." -ForegroundColor Yellow
Invoke-Setup
return
}
# Update pip
Write-Host "`n1. Upgrading pip..." -ForegroundColor Cyan
try {
Invoke-VenvCommand "-m pip install --upgrade pip" -Silent
Write-Host " ✓ Pip upgraded" -ForegroundColor Green
} catch {
Write-Host " ⚠️ Pip upgrade failed" -ForegroundColor Yellow
}
# Update dependencies
Write-Host "`n2. Updating dependencies..." -ForegroundColor Cyan
try {
Invoke-VenvCommand "-m pip install --upgrade -r $RequirementsFile" -Silent
Write-Host " ✓ Dependencies updated" -ForegroundColor Green
} catch {
throw "Failed to update dependencies: $_"
}
Write-Host "`n✅ Python environment updated!" -ForegroundColor Green
}
function Invoke-Clean {
Write-Host "`n🧹 Cleaning Python environment..." -ForegroundColor Yellow
if (-not $Force) {
$confirmation = Read-Host "This will remove the virtual environment and rebuild it. Continue? (y/N)"
if ($confirmation -notmatch "^[Yy]") {
Write-Host " Operation cancelled" -ForegroundColor Yellow
return
}
}
# Remove virtual environment
if (Test-Path $VenvPath) {
Write-Host "`n1. Removing existing virtual environment..." -ForegroundColor Cyan
Remove-Item $VenvPath -Recurse -Force
Write-Host " ✓ Virtual environment removed" -ForegroundColor Green
}
# Run setup
Write-Host "`n2. Rebuilding environment..." -ForegroundColor Cyan
Invoke-Setup
}
function Invoke-Diagnose {
Write-Host "`n🔬 Running diagnostics..." -ForegroundColor Yellow
# System information
Write-Host "`n📋 System Information:" -ForegroundColor Cyan
Write-Host " OS: $([Environment]::OSVersion.VersionString)" -ForegroundColor White
Write-Host " PowerShell: $($PSVersionTable.PSVersion)" -ForegroundColor White
$pythonInfo = Get-PythonVersion
if ($pythonInfo) {
Write-Host " Python: $($pythonInfo.Full)" -ForegroundColor White
Write-Host " Python Path: $(Get-Command python).Source" -ForegroundColor White
} else {
Write-Host " Python: Not found" -ForegroundColor Red
}
# Pyenv information
Write-Host "`n🐍 Pyenv Information:" -ForegroundColor Cyan
if (Test-Command "pyenv") {
Write-Host " ✓ pyenv available" -ForegroundColor Green
Write-Host " Version: $(pyenv --version)" -ForegroundColor White
Write-Host "`n Installed versions:" -ForegroundColor Gray
& pyenv versions | ForEach-Object { Write-Host " $_" -ForegroundColor White }
$localVersion = Get-Content (Join-Path $ProjectRoot ".python-version") -ErrorAction SilentlyContinue
if ($localVersion) {
Write-Host " Local version (.python-version): $localVersion" -ForegroundColor White
}
} else {
Write-Host " ❌ pyenv not found" -ForegroundColor Red
}
# Virtual environment information
Write-Host "`n🏠 Virtual Environment:" -ForegroundColor Cyan
if (Test-VirtualEnvironment) {
Write-Host " ✓ Virtual environment exists at: $VenvPath" -ForegroundColor Green
try {
$venvPython = Invoke-VenvCommand "--version" -Silent
Write-Host " Python version: $venvPython" -ForegroundColor White
try {
$pipVersion = Invoke-VenvCommand "-m pip --version" -Silent
Write-Host " Pip version: $pipVersion" -ForegroundColor White
} catch {
Write-Host " ⚠️ Could not get pip version: $($_.Exception.Message)" -ForegroundColor Yellow
}
$packageCount = (Invoke-VenvCommand "-m pip list --format=json" -Silent | ConvertFrom-Json).Count
Write-Host " Installed packages: $packageCount" -ForegroundColor White
} catch {
Write-Host " ⚠️ Error getting virtual environment details: $_" -ForegroundColor Yellow
}
} else {
Write-Host " ❌ Virtual environment not found" -ForegroundColor Red
}
# Configuration files
Write-Host "`n⚙️ Configuration Files:" -ForegroundColor Cyan
$configFiles = @(
@{Path=$RequirementsFile; Name="requirements.txt"},
@{Path=$PyrightConfigFile; Name="pyrightconfig.json"},
@{Path=$VscodeSettingsFile; Name=".vscode/settings.json"},
@{Path=(Join-Path $ProjectRoot ".python-version"); Name=".python-version"}
)
foreach ($file in $configFiles) {
if (Test-Path $file.Path) {
Write-Host " ✓ $($file.Name) exists" -ForegroundColor Green
} else {
Write-Host " ❌ $($file.Name) missing" -ForegroundColor Red
}
}
# Run verification
Write-Host "`n🔍 Running verification..." -ForegroundColor Cyan
$isHealthy = Invoke-Verify
if ($isHealthy) {
Write-Host "`n✅ System is healthy!" -ForegroundColor Green
} else {
Write-Host "`n❌ System has issues that need attention!" -ForegroundColor Red
}
}
#endregion
#region Main Logic
try {
if ($Setup) {
Invoke-Setup
} elseif ($Update) {
Invoke-Update
} elseif ($Clean) {
Invoke-Clean
} elseif ($Diagnose) {
Invoke-Diagnose
} elseif ($Verify) {
$isHealthy = Invoke-Verify
exit [int](-not $isHealthy)
} else {
# Default: Run diagnostics
Invoke-Diagnose
}
} catch {
Write-Host "`n❌ Error: $_" -ForegroundColor Red
Write-Host "Stack trace:" -ForegroundColor Red
Write-Host $_.ScriptStackTrace -ForegroundColor Gray
exit 1
}
Write-Host "`n🎉 Operation completed successfully!" -ForegroundColor Green
#endregion