|
| 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 | +$MINICONDA_URL = "http://repo.continuum.io/miniconda/" |
| 6 | +$BASE_URL = "https://www.python.org/ftp/python/" |
| 7 | +$GET_PIP_URL = "https://bootstrap.pypa.io/get-pip.py" |
| 8 | +$GET_PIP_PATH = "C:\get-pip.py" |
| 9 | + |
| 10 | + |
| 11 | +function DownloadPython ($python_version, $platform_suffix) { |
| 12 | + $webclient = New-Object System.Net.WebClient |
| 13 | + $filename = "python-" + $python_version + $platform_suffix + ".msi" |
| 14 | + $url = $BASE_URL + $python_version + "/" + $filename |
| 15 | + |
| 16 | + $basedir = $pwd.Path + "\" |
| 17 | + $filepath = $basedir + $filename |
| 18 | + if (Test-Path $filename) { |
| 19 | + Write-Host "Reusing" $filepath |
| 20 | + return $filepath |
| 21 | + } |
| 22 | + |
| 23 | + # Download and retry up to 3 times in case of network transient errors. |
| 24 | + Write-Host "Downloading" $filename "from" $url |
| 25 | + $retry_attempts = 2 |
| 26 | + for($i=0; $i -lt $retry_attempts; $i++){ |
| 27 | + try { |
| 28 | + $webclient.DownloadFile($url, $filepath) |
| 29 | + break |
| 30 | + } |
| 31 | + Catch [Exception]{ |
| 32 | + Start-Sleep 1 |
| 33 | + } |
| 34 | + } |
| 35 | + if (Test-Path $filepath) { |
| 36 | + Write-Host "File saved at" $filepath |
| 37 | + } else { |
| 38 | + # Retry once to get the error message if any at the last try |
| 39 | + $webclient.DownloadFile($url, $filepath) |
| 40 | + } |
| 41 | + return $filepath |
| 42 | +} |
| 43 | + |
| 44 | + |
| 45 | +function InstallPython ($python_version, $architecture, $python_home) { |
| 46 | + Write-Host "Installing Python" $python_version "for" $architecture "bit architecture to" $python_home |
| 47 | + if (Test-Path $python_home) { |
| 48 | + Write-Host $python_home "already exists, skipping." |
| 49 | + return $false |
| 50 | + } |
| 51 | + if ($architecture -eq "32") { |
| 52 | + $platform_suffix = "" |
| 53 | + } else { |
| 54 | + $platform_suffix = ".amd64" |
| 55 | + } |
| 56 | + $msipath = DownloadPython $python_version $platform_suffix |
| 57 | + Write-Host "Installing" $msipath "to" $python_home |
| 58 | + $install_log = $python_home + ".log" |
| 59 | + $install_args = "/qn /log $install_log /i $msipath TARGETDIR=$python_home" |
| 60 | + $uninstall_args = "/qn /x $msipath" |
| 61 | + RunCommand "msiexec.exe" $install_args |
| 62 | + if (-not(Test-Path $python_home)) { |
| 63 | + Write-Host "Python seems to be installed else-where, reinstalling." |
| 64 | + RunCommand "msiexec.exe" $uninstall_args |
| 65 | + RunCommand "msiexec.exe" $install_args |
| 66 | + } |
| 67 | + if (Test-Path $python_home) { |
| 68 | + Write-Host "Python $python_version ($architecture) installation complete" |
| 69 | + } else { |
| 70 | + Write-Host "Failed to install Python in $python_home" |
| 71 | + Get-Content -Path $install_log |
| 72 | + Exit 1 |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +function RunCommand ($command, $command_args) { |
| 77 | + Write-Host $command $command_args |
| 78 | + Start-Process -FilePath $command -ArgumentList $command_args -Wait -Passthru |
| 79 | +} |
| 80 | + |
| 81 | + |
| 82 | +function InstallPip ($python_home) { |
| 83 | + $pip_path = $python_home + "\Scripts\pip.exe" |
| 84 | + $python_path = $python_home + "\python.exe" |
| 85 | + if (-not(Test-Path $pip_path)) { |
| 86 | + Write-Host "Installing pip..." |
| 87 | + $webclient = New-Object System.Net.WebClient |
| 88 | + $webclient.DownloadFile($GET_PIP_URL, $GET_PIP_PATH) |
| 89 | + Write-Host "Executing:" $python_path $GET_PIP_PATH |
| 90 | + Start-Process -FilePath "$python_path" -ArgumentList "$GET_PIP_PATH" -Wait -Passthru |
| 91 | + } else { |
| 92 | + Write-Host "pip already installed." |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | + |
| 97 | +function DownloadMiniconda ($python_version, $platform_suffix) { |
| 98 | + $webclient = New-Object System.Net.WebClient |
| 99 | + if ($python_version -eq "3.4") { |
| 100 | + $filename = "Miniconda3-3.5.5-Windows-" + $platform_suffix + ".exe" |
| 101 | + } else { |
| 102 | + $filename = "Miniconda-3.5.5-Windows-" + $platform_suffix + ".exe" |
| 103 | + } |
| 104 | + $url = $MINICONDA_URL + $filename |
| 105 | + |
| 106 | + $basedir = $pwd.Path + "\" |
| 107 | + $filepath = $basedir + $filename |
| 108 | + if (Test-Path $filename) { |
| 109 | + Write-Host "Reusing" $filepath |
| 110 | + return $filepath |
| 111 | + } |
| 112 | + |
| 113 | + # Download and retry up to 3 times in case of network transient errors. |
| 114 | + Write-Host "Downloading" $filename "from" $url |
| 115 | + $retry_attempts = 2 |
| 116 | + for($i=0; $i -lt $retry_attempts; $i++){ |
| 117 | + try { |
| 118 | + $webclient.DownloadFile($url, $filepath) |
| 119 | + break |
| 120 | + } |
| 121 | + Catch [Exception]{ |
| 122 | + Start-Sleep 1 |
| 123 | + } |
| 124 | + } |
| 125 | + if (Test-Path $filepath) { |
| 126 | + Write-Host "File saved at" $filepath |
| 127 | + } else { |
| 128 | + # Retry once to get the error message if any at the last try |
| 129 | + $webclient.DownloadFile($url, $filepath) |
| 130 | + } |
| 131 | + return $filepath |
| 132 | +} |
| 133 | + |
| 134 | + |
| 135 | +function InstallMiniconda ($python_version, $architecture, $python_home) { |
| 136 | + Write-Host "Installing Python" $python_version "for" $architecture "bit architecture to" $python_home |
| 137 | + if (Test-Path $python_home) { |
| 138 | + Write-Host $python_home "already exists, skipping." |
| 139 | + return $false |
| 140 | + } |
| 141 | + if ($architecture -eq "32") { |
| 142 | + $platform_suffix = "x86" |
| 143 | + } else { |
| 144 | + $platform_suffix = "x86_64" |
| 145 | + } |
| 146 | + $filepath = DownloadMiniconda $python_version $platform_suffix |
| 147 | + Write-Host "Installing" $filepath "to" $python_home |
| 148 | + $install_log = $python_home + ".log" |
| 149 | + $args = "/S /D=$python_home" |
| 150 | + Write-Host $filepath $args |
| 151 | + Start-Process -FilePath $filepath -ArgumentList $args -Wait -Passthru |
| 152 | + if (Test-Path $python_home) { |
| 153 | + Write-Host "Python $python_version ($architecture) installation complete" |
| 154 | + } else { |
| 155 | + Write-Host "Failed to install Python in $python_home" |
| 156 | + Get-Content -Path $install_log |
| 157 | + Exit 1 |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | + |
| 162 | +function InstallMinicondaPip ($python_home) { |
| 163 | + $pip_path = $python_home + "\Scripts\pip.exe" |
| 164 | + $conda_path = $python_home + "\Scripts\conda.exe" |
| 165 | + if (-not(Test-Path $pip_path)) { |
| 166 | + Write-Host "Installing pip..." |
| 167 | + $args = "install --yes pip" |
| 168 | + Write-Host $conda_path $args |
| 169 | + Start-Process -FilePath "$conda_path" -ArgumentList $args -Wait -Passthru |
| 170 | + } else { |
| 171 | + Write-Host "pip already installed." |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +function main () { |
| 176 | + InstallPython $env:PYTHON_VERSION $env:PYTHON_ARCH $env:PYTHON |
| 177 | + InstallPip $env:PYTHON |
| 178 | +} |
| 179 | + |
| 180 | +main |
0 commit comments