File size: 1,596 Bytes
0e6535c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# 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"