Skip to content

Instantly share code, notes, and snippets.

@aligusnet
Created February 18, 2019 22:06
Show Gist options
  • Save aligusnet/39850af089b3e2ccac997fbcf2e06238 to your computer and use it in GitHub Desktop.
Save aligusnet/39850af089b3e2ccac997fbcf2e06238 to your computer and use it in GitHub Desktop.
Enable-Python script allows to switch between differen versions of Python installed on Windows machine
# copy the code to $PROFILE.CurrentUserAllHosts
# and restard PowerShell.
# Now you can switch between different versions of Python using EnablePython <Version>
# or short aliases: py36, py37, py38
Function Get-OriginalPath {
if (-not $env:ORIGINAL_PATH) {
$env:ORIGINAL_PATH = $env:PATH
}
return $env:ORIGINAL_PATH
}
Function Get-PythonBasePath {
$env:LOCALAPPDATA + '\Programs\Python'
}
Function Get-AvailablePythons {
Get-PythonBasePath | Get-ChildItem | Foreach-Object {
$_.Name.ToString()
}
}
Function Enable-Python ([string] $Version) {
$AvailableVersions = Get-AvailablePythons
if ($Version -in $AvailableVersions) {
$originalPath = Get-OriginalPath
$basePythonPath = Get-PythonBasePath
$pythonPath = $basePythonPath + "\" + $Version
$pythonScriptsPath = $pythonPath + "\Scripts"
$env:Path = $pythonPath + ";" + $pythonScriptsPath + ";" + $originalPath
} else {
Write-Output "$Version is not available. Please select one of $AvailableVersions"
}
}
Function py36 {
Enable-Python Python36
}
Function py37 {
Enable-Python Python37
}
Function py38 {
Enable-Python Python38
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment