Last active
January 8, 2024 23:12
-
-
Save AfroThundr3007730/971315153de03a9b01331644310d0e9a to your computer and use it in GitHub Desktop.
Install the Python language interpreter
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
Set-StrictMode -Version Latest | |
#Requires -Version 7 | |
function Install-PythonRelease { | |
<# .SYNOPSIS | |
Install the Python language interpreter #> | |
param( | |
# Install for all users (requires admin) | |
[Switch]$AllUsers, | |
# Use beta build instead of latest stable | |
[Switch]$Beta, | |
# Use version X.Y.Z instead of latest stable | |
[Version]$Version | |
) | |
if ($AllUsers -and !( | |
[Security.Principal.WindowsIdentity]::GetCurrent().groups -contains 'S-1-5-32-544')) { | |
throw 'The AllUsers switch requires administrator privileges.' | |
} | |
[Uri]$baseURI = 'https://www.python.org/ftp/python/' | |
if (!$Version) { | |
[Version[]]$versionList = ( | |
Invoke-WebRequest -NoProxy -Uri $baseURI | |
).links.href.where{ $_ -match '\d+\.\d+\.\d+' }.trim('/') | |
[Array]::sort($versionList) | |
$Version = $versionList[$($Beta ? -1 : -2)] | |
} | |
$installDir = $AllUsers ? | |
[IO.DirectoryInfo][IO.Path]::Join( | |
[Environment]::GetFolderPath('ProgramFiles'), 'Python', | |
('Python {0:d}.{1:d}' -f $Version.Major, $Version.Minor)) : | |
[IO.DirectoryInfo][IO.Path]::Join( | |
[Environment]::GetFolderPath('LocalApplicationData'), 'Programs', 'Python', | |
('Python{0:d}{1:d}' -f $Version.Major, $Version.Minor)) | |
if ($installDir.Exists) { | |
throw 'Selected python release already exists at {0:s}' -f $installDir | |
} | |
[Uri]$download = $Beta ? | |
'{0:s}{1:s}/{2:s}' -f $baseURI, $Version, ( | |
Invoke-WebRequest -NoProxy -Uri ('{0:s}{1:s}' -f $baseURI, $Version) | |
).links.href.where{ $_ -match 'amd64\.exe$' }[-1] : | |
'{0:s}{1:s}/python-{1:s}-amd64.exe' -f $baseURI, $Version | |
$setup = [IO.Path]::Join([IO.Path]::GetTempPath(), $download.Segments[-1]) | |
Invoke-WebRequest -NoProxy -Uri $download -OutFile $setup | |
if (!([IO.FileInfo]$setup).Exists) { | |
throw 'Setup file {0:s} not downloaded' -f $setup | |
} | |
& $setup /quiet InstallAllUsers=$($AllUsers ? 1 : 0) PrependPath=1 Include_launcher=0 Shortcuts=0 | |
([IO.FileInfo]$setup).Delete() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment