|
| 1 | +# Sample script to install Python and pip under Windows |
| 2 | +# Authors: Olivier Grisel, Jonathan Helmus and Kyle Kastner |
| 3 | +# License: CC0 1.0 Universal: http://creativecommons.org/publicdomain/zero/1.0/ |
| 4 | + |
| 5 | +$BASE_URL = "https://www.python.org/ftp/python/" |
| 6 | +$GET_PIP_URL = "https://bootstrap.pypa.io/get-pip.py" |
| 7 | +$GET_PIP_PATH = "C:\get-pip.py" |
| 8 | + |
| 9 | + |
| 10 | +function DownloadPython ($python_version, $platform_suffix) { |
| 11 | + $webclient = New-Object System.Net.WebClient |
| 12 | + $filename = "python-" + $python_version + $platform_suffix + ".msi" |
| 13 | + $url = $BASE_URL + $python_version + "/" + $filename |
| 14 | + |
| 15 | + $basedir = $pwd.Path + "\" |
| 16 | + $filepath = $basedir + $filename |
| 17 | + if (Test-Path $filename) { |
| 18 | + Write-Host "Reusing" $filepath |
| 19 | + return $filepath |
| 20 | + } |
| 21 | + |
| 22 | + # Download and retry up to 3 times in case of network transient errors. |
| 23 | + Write-Host "Downloading" $filename "from" $url |
| 24 | + $retry_attempts = 2 |
| 25 | + for($i=0; $i -lt $retry_attempts; $i++){ |
| 26 | + try { |
| 27 | + $webclient.DownloadFile($url, $filepath) |
| 28 | + break |
| 29 | + } |
| 30 | + Catch [Exception]{ |
| 31 | + Start-Sleep 1 |
| 32 | + } |
| 33 | + } |
| 34 | + if (Test-Path $filepath) { |
| 35 | + Write-Host "File saved at" $filepath |
| 36 | + } else { |
| 37 | + # Retry once to get the error message if any at the last try |
| 38 | + $webclient.DownloadFile($url, $filepath) |
| 39 | + } |
| 40 | + return $filepath |
| 41 | +} |
| 42 | + |
| 43 | + |
| 44 | +function InstallPython ($python_version, $architecture, $python_home) { |
| 45 | + Write-Host "Installing Python" $python_version "for" $architecture "bit architecture to" $python_home |
| 46 | + if (Test-Path $python_home) { |
| 47 | + Write-Host $python_home "already exists, skipping." |
| 48 | + return $false |
| 49 | + } |
| 50 | + if ($architecture -eq "32") { |
| 51 | + $platform_suffix = "" |
| 52 | + } else { |
| 53 | + $platform_suffix = ".amd64" |
| 54 | + } |
| 55 | + $msipath = DownloadPython $python_version $platform_suffix |
| 56 | + Write-Host "Installing" $msipath "to" $python_home |
| 57 | + $install_log = $python_home + ".log" |
| 58 | + $install_args = "/qn /log $install_log /i $msipath TARGETDIR=$python_home" |
| 59 | + $uninstall_args = "/qn /x $msipath" |
| 60 | + RunCommand "msiexec.exe" $install_args |
| 61 | + if (-not(Test-Path $python_home)) { |
| 62 | + Write-Host "Python seems to be installed else-where, reinstalling." |
| 63 | + RunCommand "msiexec.exe" $uninstall_args |
| 64 | + RunCommand "msiexec.exe" $install_args |
| 65 | + } |
| 66 | + if (Test-Path $python_home) { |
| 67 | + Write-Host "Python $python_version ($architecture) installation complete" |
| 68 | + } else { |
| 69 | + Write-Host "Failed to install Python in $python_home" |
| 70 | + Get-Content -Path $install_log |
| 71 | + Exit 1 |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +function RunCommand ($command, $command_args) { |
| 76 | + Write-Host $command $command_args |
| 77 | + Start-Process -FilePath $command -ArgumentList $command_args -Wait -Passthru |
| 78 | +} |
| 79 | + |
| 80 | + |
| 81 | +function InstallPip ($python_home) { |
| 82 | + $pip_path = $python_home + "\Scripts\pip.exe" |
| 83 | + $python_path = $python_home + "\python.exe" |
| 84 | + if (-not(Test-Path $pip_path)) { |
| 85 | + Write-Host "Installing pip..." |
| 86 | + $webclient = New-Object System.Net.WebClient |
| 87 | + $webclient.DownloadFile($GET_PIP_URL, $GET_PIP_PATH) |
| 88 | + Write-Host "Executing:" $python_path $GET_PIP_PATH |
| 89 | + Start-Process -FilePath "$python_path" -ArgumentList "$GET_PIP_PATH" -Wait -Passthru |
| 90 | + } else { |
| 91 | + Write-Host "pip already installed." |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | + |
| 96 | +function main () { |
| 97 | + InstallPython $env:PYTHON_VERSION $env:PYTHON_ARCH $env:PYTHON |
| 98 | + InstallPip $env:PYTHON |
| 99 | +} |
| 100 | + |
| 101 | +main |
0 commit comments