Skip to content

Instantly share code, notes, and snippets.

@KaBankz
Last active July 14, 2021 20:50
Show Gist options
  • Save KaBankz/6580f2164c7e7987e91ab845d1bdabb2 to your computer and use it in GitHub Desktop.
Save KaBankz/6580f2164c7e7987e91ab845d1bdabb2 to your computer and use it in GitHub Desktop.
This script is not necessary for pyenv >= 2.64.8 | Add registery keys for globally set python version for pyenv-win
Write-Host @"
+===================================================+
| This script is not necessary for pyenv >= 2.64.8 |
| Use 'pyenv install VERSION --register' instead |
+===================================================+
"@ -ForegroundColor Red
$LogTimeStamp = Get-Date -Format "MM.dd.yy.hh.mm.ss"
function Write-HostAndLog {
Param(
$Message,
$ForegroundColor = "White",
$Path = "$HOME\Desktop\addPyenvRegKeys.log.$LogTimeStamp.txt"
)
Write-Host $Message -ForegroundColor $ForegroundColor
"[$(Get-Date -Format "hh:mm:ss")] $Message" | Tee-Object -FilePath $Path -Append | Write-Verbose
}
Function Quit() {
param(
[Parameter(Mandatory = $false)] [string] $ExitMessage
)
Write-HostAndLog "Exiting - $ExitMessage" -ForegroundColor "Red"
Write-Host "Log file saved at '$HOME\Desktop\addPyenvRegKeys.log.$LogTimeStamp.txt'"
Read-Host "Press any key to exit..."
Break Script
}
try {
if (-not (Get-Command "pyenv" -ErrorAction SilentlyContinue)) { throw "Pyenv not installed" }
$PyenvVersion = (pyenv --version)
Write-HostAndLog "Found $PyenvVersion" -ForegroundColor "Green"
$GlobalPythonVersion = (pyenv global)
if ($GlobalPythonVersion.ToLower() -eq "no global version configured") { throw "No global python version set" }
Write-HostAndLog "Found python $GlobalPythonVersion" -ForegroundColor "Green"
Write-HostAndLog "Starting"
$InstallCaption = Write-Host "This will create registry keys for python $GlobalPythonVersion" -ForegroundColor DarkYellow
$InstallMessage = "Are you sure you want to proceed?"
$InstallChoices = New-Object Collections.ObjectModel.Collection[Management.Automation.Host.ChoiceDescription]
$InstallChoices.Add((New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", "Create registry keys for python $GlobalPythonVersion"))
$InstallChoices.Add((New-Object System.Management.Automation.Host.ChoiceDescription "&No", "Quit and do not modify the system"))
$InstallPrompt = $Host.UI.PromptForChoice($InstallCaption, $InstallMessage, $InstallChoices, 1)
switch ($InstallPrompt) {
0 {
if (Test-Path "HKCU:\Software\Python\PythonCore\$GlobalPythonVersion" -errorAction SilentlyContinue) {
$OverwriteCaption = Write-Warning "Registry keys already exist for python $GlobalPythonVersion!"
$OverwriteMessage = "Do you want to overwrite them?"
$OverwriteChoices = New-Object Collections.ObjectModel.Collection[Management.Automation.Host.ChoiceDescription]
$OverwriteChoices.Add((New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", "Overwrite existing registry keys for python $GlobalPythonVersion"))
$OverwriteChoices.Add((New-Object System.Management.Automation.Host.ChoiceDescription "&No", "Quit and do not modify the system"))
$OverwritePrompt = $Host.UI.PromptForChoice($OverwriteCaption, $OverwriteMessage, $OverwriteChoices, 1)
switch ($OverwritePrompt) {
0 { Continue }
1 { Quit "Did not modify the system" }
}
}
Write-HostAndLog "Creating registry keys for python $GlobalPythonVersion" -ForegroundColor "Green"
$GlobalPythonExecutable = (pyenv which python)
$GlobalPythonWindowedExecutable = (pyenv which python).replace('python.exe', 'pythonw.exe')
$GlobalPythonPath = (pyenv which python).replace('\python.exe', '')
New-Item "HKCU:\Software\Python\PythonCore\$GlobalPythonVersion" -Force |
New-ItemProperty -Name "DisplayName" -Value "Python $GlobalPythonVersion" -Force |
New-ItemProperty -Name "Version" -Value "$GlobalPythonVersion" -Force |
New-ItemProperty -Name "SupportUrl" -Value "https://github.com/pyenv-win/pyenv-win/issues" -Force | Out-Null
Write-HostAndLog "Created: HKCU:\Software\Python\PythonCore\$GlobalPythonVersion"
Write-HostAndLog "Created: HKCU:\Software\Python\PythonCore\$GlobalPythonVersion\DisplayName = Python $GlobalPythonVersion"
Write-HostAndLog "Created: HKCU:\Software\Python\PythonCore\$GlobalPythonVersion\Version = $GlobalPythonVersion"
Write-HostAndLog "Created: HKCU:\Software\Python\PythonCore\$GlobalPythonVersion\SupportUrl = https://github.com/pyenv-win/pyenv-win/issues"
New-Item "HKCU:\Software\Python\PythonCore\$GlobalPythonVersion\InstalledFeatures" -Force |
New-ItemProperty -Name "dev" -Value "$GlobalPythonVersion" -Force |
New-ItemProperty -Name "exe" -Value "$GlobalPythonVersion" -Force |
New-ItemProperty -Name "lib" -Value "$GlobalPythonVersion" -Force |
New-ItemProperty -Name "pip" -Value "$GlobalPythonVersion" -Force |
New-ItemProperty -Name "tools" -Value "$GlobalPythonVersion" -Force | Out-Null
Write-HostAndLog "Created: HKCU:\Software\Python\PythonCore\$GlobalPythonVersion\InstalledFeatures"
Write-HostAndLog "Created: HkCU:\Software\Python\PythonCore\$GlobalPythonVersion\InstalledFeatures\dev = $GlobalPythonVersion"
Write-HostAndLog "Created: HkCU:\Software\Python\PythonCore\$GlobalPythonVersion\InstalledFeatures\exe = $GlobalPythonVersion"
Write-HostAndLog "Created: HkCU:\Software\Python\PythonCore\$GlobalPythonVersion\InstalledFeatures\lib = $GlobalPythonVersion"
Write-HostAndLog "Created: HkCU:\Software\Python\PythonCore\$GlobalPythonVersion\InstalledFeatures\pip = $GlobalPythonVersion"
Write-HostAndLog "Created: HkCU:\Software\Python\PythonCore\$GlobalPythonVersion\InstalledFeatures\tools = $GlobalPythonVersion"
New-Item "HKCU:\Software\Python\PythonCore\$GlobalPythonVersion\InstallPath" -Force |
New-ItemProperty -Name "(Default)" -Value "$GlobalPythonPath\" -Force |
New-ItemProperty -Name "ExecutablePath" -Value "$GlobalPythonExecutable" -Force |
New-ItemProperty -Name "WindowedExecutablePath" -Value "$GlobalPythonWindowedExecutable" -Force | Out-Null
Write-HostAndLog "Created: HKCU:\Software\Python\PythonCore\$GlobalPythonVersion\InstallPath"
Write-HostAndLog "Created: HKCU:\Software\Python\PythonCore\$GlobalPythonVersion\InstallPath\(Default) = $GlobalPythonPath\"
Write-HostAndLog "Created: HKCU:\Software\Python\PythonCore\$GlobalPythonVersion\InstallPath\ExecutablePath = $GlobalPythonExecutable"
Write-HostAndLog "Created: HKCU:\Software\Python\PythonCore\$GlobalPythonVersion\InstallPath\WindowedExecutablePath = $GlobalPythonWindowedExecutable"
New-Item "HKCU:\Software\Python\PythonCore\$GlobalPythonVersion\PythonPath" -Force |
New-ItemProperty -Name "(Default)" -Value "$GlobalPythonPath\Lib\;$GlobalPythonPath\DLLs\" -Force | Out-Null
Write-HostAndLog "Created: HKCU:\Software\Python\PythonCore\$GlobalPythonVersion\PythonPath"
Write-HostAndLog "Created: HKCU:\Software\Python\PythonCore\$GlobalPythonVersion\PythonPath\(Default) = $GlobalPythonPath\Lib\;$GlobalPythonPath\DLLs\"
Write-HostAndLog "Success - Created registry keys for python $GlobalPythonVersion" -ForegroundColor "Green"
Quit "System has been successfully modified"
}
1 { Quit "Did not modify the system" }
}
}
catch {
Quit $_
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment