-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_switcher.ps1
More file actions
325 lines (281 loc) · 12.8 KB
/
python_switcher.ps1
File metadata and controls
325 lines (281 loc) · 12.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
# Python Version Switcher - PowerShell Script
# Works with any Python version and is future-proof
param()
# Set console properties for better visibility
$Host.UI.RawUI.WindowTitle = "Python Version Switcher"
Clear-Host
Write-Host "========================================" -ForegroundColor Green
Write-Host " Python Version Switcher v2.0" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green
Write-Host ""
# Function to get Python version from executable
function Get-PythonVersion {
param([string]$PythonPath)
try {
$version = & "$PythonPath" --version 2>&1
if ($version -match "Python\s+(\d+\.\d+\.\d+)") {
return $matches[1]
}
return "Unknown"
}
catch {
return "Unknown"
}
}
# Function to check if path is already in PATH environment variable
function Test-PathInEnvironment {
param([string]$TestPath)
$currentPath = [Environment]::GetEnvironmentVariable("PATH", "User")
return $currentPath -split ";" -contains $TestPath
}
try {
# Show current Python version
Write-Host "Current Python version:" -ForegroundColor Cyan
try {
$currentVersion = python --version 2>&1
Write-Host $currentVersion -ForegroundColor White
}
catch {
Write-Host "No Python found in current PATH" -ForegroundColor Yellow
}
Write-Host ""
# Array to store Python installations
$pythonInstallations = @()
Write-Host "Scanning for Python installations..." -ForegroundColor Yellow
Write-Host ""
# Define search locations (future-proof - will find any Python version)
$searchLocations = @(
"$env:LOCALAPPDATA\Programs\Python",
"$env:APPDATA\Python",
"$env:ProgramFiles\Python*",
"$env:ProgramFiles\Microsoft\WindowsApps",
"${env:ProgramFiles(x86)}\Python*",
"$env:SystemDrive\Python*"
)
# Search in user AppData (most common for newer Python installations)
if (Test-Path "$env:LOCALAPPDATA\Programs\Python") {
$userPythonDirs = Get-ChildItem "$env:LOCALAPPDATA\Programs\Python" -Directory -ErrorAction SilentlyContinue
foreach ($dir in $userPythonDirs) {
$pythonExe = Join-Path $dir.FullName "python.exe"
if (Test-Path $pythonExe) {
$version = Get-PythonVersion $pythonExe
$pythonInstallations += @{
Path = $dir.FullName
Version = $version
Executable = $pythonExe
Type = "User Installation"
}
}
}
}
# Search in system-wide locations
foreach ($location in $searchLocations) {
if ($location -like "*\*") {
# Handle wildcard paths
$basePath = Split-Path $location -Parent
$pattern = Split-Path $location -Leaf
if (Test-Path $basePath) {
$dirs = Get-ChildItem $basePath -Directory -Filter $pattern -ErrorAction SilentlyContinue
foreach ($dir in $dirs) {
$pythonExe = Join-Path $dir.FullName "python.exe"
if (Test-Path $pythonExe) {
# Avoid duplicates
$exists = $pythonInstallations | Where-Object { $_.Path -eq $dir.FullName }
if (-not $exists) {
$version = Get-PythonVersion $pythonExe
$pythonInstallations += @{
Path = $dir.FullName
Version = $version
Executable = $pythonExe
Type = "System Installation"
}
}
}
}
}
}
else {
if (Test-Path $location) {
$pythonExe = Join-Path $location "python.exe"
if (Test-Path $pythonExe) {
$exists = $pythonInstallations | Where-Object { $_.Path -eq $location }
if (-not $exists) {
$version = Get-PythonVersion $pythonExe
$pythonInstallations += @{
Path = $location
Version = $version
Executable = $pythonExe
Type = "System Installation"
}
}
}
}
}
}
# Also check for Python Launcher installations
$pyLauncher = Get-Command py -ErrorAction SilentlyContinue
if ($pyLauncher) {
try {
$pyVersions = py -0 2>&1 | Where-Object { $_ -match "^\s*-(\d+\.\d+)" }
foreach ($pyVer in $pyVersions) {
if ($pyVer -match "^\s*-(\d+\.\d+)") {
$versionNumber = $matches[1]
try {
$pythonPath = & py -$versionNumber -c "import sys; print(sys.executable)" 2>&1
if ($pythonPath -and (Test-Path $pythonPath)) {
$pythonDir = Split-Path $pythonPath -Parent
$exists = $pythonInstallations | Where-Object { $_.Path -eq $pythonDir }
if (-not $exists) {
$version = Get-PythonVersion $pythonPath
$pythonInstallations += @{
Path = $pythonDir
Version = $version
Executable = $pythonPath
Type = "Launcher Detection"
}
}
}
}
catch { }
}
}
}
catch { }
}
# Sort installations by version (newest first)
$pythonInstallations = $pythonInstallations | Sort-Object {
if ($_.Version -match "(\d+)\.(\d+)\.(\d+)") {
[version]"$($matches[1]).$($matches[2]).$($matches[3])"
} else { [version]"0.0.0" }
} -Descending
if ($pythonInstallations.Count -eq 0) {
Write-Host "No Python installations found!" -ForegroundColor Red
Write-Host "Please install Python first from https://python.org" -ForegroundColor Yellow
Write-Host ""
Write-Host "Press any key to exit..."
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
exit 1
}
Write-Host "Found $($pythonInstallations.Count) Python installation(s):" -ForegroundColor Green
Write-Host ""
for ($i = 0; $i -lt $pythonInstallations.Count; $i++) {
$installation = $pythonInstallations[$i]
$number = $i + 1
Write-Host "$number. " -NoNewline -ForegroundColor White
Write-Host "Python $($installation.Version)" -NoNewline -ForegroundColor Cyan
Write-Host " - $($installation.Path)" -ForegroundColor Gray
Write-Host " [$($installation.Type)]" -ForegroundColor DarkGray
}
Write-Host ""
Write-Host "0. " -NoNewline -ForegroundColor White
Write-Host "Exit without changes" -ForegroundColor Red
Write-Host ""
do {
$choice = Read-Host "Select Python version to set as default (0-$($pythonInstallations.Count))"
if ($choice -eq "0") {
Write-Host "No changes made." -ForegroundColor Yellow
Write-Host "Press any key to exit..."
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
exit 0
}
$choiceNum = $choice -as [int]
if ($choiceNum -ge 1 -and $choiceNum -le $pythonInstallations.Count) {
break
}
Write-Host "Invalid choice! Please enter a number between 0 and $($pythonInstallations.Count)." -ForegroundColor Red
} while ($true)
$selectedInstallation = $pythonInstallations[$choiceNum - 1]
Write-Host ""
Write-Host "Selected: " -NoNewline -ForegroundColor White
Write-Host "Python $($selectedInstallation.Version)" -NoNewline -ForegroundColor Cyan
Write-Host " - $($selectedInstallation.Path)" -ForegroundColor Gray
Write-Host ""
do {
$confirm = Read-Host "Set this as default Python? (Y/N)"
if ($confirm -match "^[Yy]([Ee][Ss])?$") {
break
}
if ($confirm -match "^[Nn][Oo]?$") {
Write-Host "Operation cancelled." -ForegroundColor Yellow
Write-Host "Press any key to exit..."
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
exit 0
}
Write-Host "Please enter Y or N." -ForegroundColor Red
} while ($true)
Write-Host ""
Write-Host "Updating PATH environment variable..." -ForegroundColor Yellow
# Get current user PATH
$currentUserPath = [Environment]::GetEnvironmentVariable("PATH", "User")
if (-not $currentUserPath) { $currentUserPath = "" }
# Split PATH into array and remove empty entries
$pathArray = $currentUserPath -split ";" | Where-Object { $_ -ne "" -and $_.Trim() -ne "" }
# Only remove the SELECTED Python paths to prevent duplicates
$selectedPythonPath = $selectedInstallation.Path.Trim()
$selectedScriptsPath = "$selectedPythonPath\Scripts"
Write-Host "Removing selected Python paths (if they exist) to prevent duplicates..." -ForegroundColor Yellow
# Count removed paths for feedback
$removedCount = 0
$originalCount = $pathArray.Count
# Remove ONLY the selected Python paths
$pathArray = $pathArray | Where-Object {
$path = $_.Trim()
$isSelectedPythonPath = ($path -eq $selectedPythonPath -or $path -eq $selectedScriptsPath)
if ($isSelectedPythonPath) {
$removedCount++
Write-Host " Removed: $path" -ForegroundColor DarkYellow
}
return -not $isSelectedPythonPath
}
if ($removedCount -gt 0) {
Write-Host "Removed $removedCount duplicate path(s)" -ForegroundColor Green
} else {
Write-Host "No existing paths found for selected Python version" -ForegroundColor Gray
}
# Add selected Python paths to the BEGINNING (highest priority)
Write-Host "Adding selected Python paths to the beginning of PATH..." -ForegroundColor Yellow
$newPathArray = @($selectedPythonPath, $selectedScriptsPath) + $pathArray
Write-Host " Added to top: $selectedPythonPath" -ForegroundColor Green
Write-Host " Added to top: $selectedScriptsPath" -ForegroundColor Green
# Join back to string and clean up any double semicolons
$newPath = ($newPathArray -join ";") -replace ";;+", ";"
$newPath = $newPath.Trim(";")
# Update user PATH environment variable
[Environment]::SetEnvironmentVariable("PATH", $newPath, "User")
Write-Host ""
Write-Host "========================================" -ForegroundColor Green
Write-Host "PATH updated successfully!" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green
Write-Host ""
Write-Host "Selected Python version: " -NoNewline -ForegroundColor White
Write-Host $selectedInstallation.Version -ForegroundColor Cyan
Write-Host "Python path: " -NoNewline -ForegroundColor White
Write-Host $selectedInstallation.Path -ForegroundColor Gray
Write-Host ""
Write-Host "NOTE: You may need to:" -ForegroundColor Yellow
Write-Host "1. Restart your command prompt/terminal" -ForegroundColor Yellow
Write-Host "2. Or start a new PowerShell session" -ForegroundColor Yellow
Write-Host "3. Changes take effect immediately in new sessions" -ForegroundColor Yellow
Write-Host ""
# Update PATH for current session
$env:PATH = "$($selectedInstallation.Path);$($selectedInstallation.Path)\Scripts;$env:PATH"
Write-Host "Testing new Python version:" -ForegroundColor Cyan
try {
$testVersion = & "$($selectedInstallation.Executable)" --version 2>&1
Write-Host $testVersion -ForegroundColor Green
}
catch {
Write-Host "Could not test Python version, but PATH was updated." -ForegroundColor Yellow
}
Write-Host ""
}
catch {
Write-Host ""
Write-Host "An error occurred:" -ForegroundColor Red
Write-Host $_.Exception.Message -ForegroundColor Red
Write-Host ""
Write-Host "Please run this script as Administrator if the error persists." -ForegroundColor Yellow
Write-Host ""
}
Write-Host "Press any key to exit..." -ForegroundColor Gray
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")