Created
February 18, 2019 22:06
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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