Spaces:
Running
Running
| # File: setup_python_env.ps1 | |
| $ErrorActionPreference = "Stop" | |
| function Test-Python310 { | |
| try { | |
| $version = python -c "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')" | |
| if ($version -eq "3.10") { | |
| return $true | |
| } | |
| } catch { | |
| return $false | |
| } | |
| return $false | |
| } | |
| function Install-Python310 { | |
| Write-Host "Installing Python 3.10 via winget..." | |
| winget install -e --id Python.Python.3.10 --source winget | |
| } | |
| function Create-Venv { | |
| param([string]$VenvPath) | |
| Write-Host "Creating virtual environment at $VenvPath" | |
| python -m venv $VenvPath | |
| } | |
| function Activate-Venv { | |
| param([string]$VenvPath) | |
| $activateScript = Join-Path $VenvPath "Scripts\Activate.ps1" | |
| if (!(Test-Path $activateScript)) { | |
| throw "Activation script not found." | |
| } | |
| Write-Host "Activating virtual environment..." | |
| & $activateScript | |
| } | |
| function Install-BasePackages { | |
| Write-Host "Upgrading pip tools..." | |
| python -m pip install --upgrade pip setuptools wheel | |
| } | |
| function Install-Requirements { | |
| if (Test-Path "requirements.txt") { | |
| Write-Host "Installing requirements..." | |
| pip install -r requirements.txt | |
| } | |
| } | |
| Write-Host "Checking Python 3.10..." | |
| if (!(Test-Python310)) { | |
| Install-Python310 | |
| } | |
| $venvPath = ".venv" | |
| if (!(Test-Path $venvPath)) { | |
| Create-Venv -VenvPath $venvPath | |
| } | |
| Activate-Venv -VenvPath $venvPath | |
| Install-BasePackages | |
| Install-Requirements | |
| Write-Host "" | |
| Write-Host "Environment ready!" | |
| Write-Host "To activate later run:" | |
| Write-Host ".\.venv\Scripts\Activate.ps1" |