install.ps1 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # Sample script to install Python and pip under Windows
  2. # Authors: Olivier Grisel and Kyle Kastner
  3. # License: CC0 1.0 Universal: http://creativecommons.org/publicdomain/zero/1.0/
  4. $BASE_URL = "https://www.python.org/ftp/python/"
  5. $GET_PIP_URL = "https://bootstrap.pypa.io/get-pip.py"
  6. $GET_PIP_PATH = "C:\get-pip.py"
  7. function DownloadPython ($python_version, $platform_suffix) {
  8. $webclient = New-Object System.Net.WebClient
  9. $filename = "python-" + $python_version + $platform_suffix + ".msi"
  10. $url = $BASE_URL + $python_version + "/" + $filename
  11. $basedir = $pwd.Path + "\"
  12. $filepath = $basedir + $filename
  13. if (Test-Path $filename) {
  14. Write-Host "Reusing" $filepath
  15. return $filepath
  16. }
  17. # Download and retry up to 5 times in case of network transient errors.
  18. Write-Host "Downloading" $filename "from" $url
  19. $retry_attempts = 3
  20. for($i=0; $i -lt $retry_attempts; $i++){
  21. try {
  22. $webclient.DownloadFile($url, $filepath)
  23. break
  24. }
  25. Catch [Exception]{
  26. Start-Sleep 1
  27. }
  28. }
  29. Write-Host "File saved at" $filepath
  30. return $filepath
  31. }
  32. function InstallPython ($python_version, $architecture, $python_home) {
  33. Write-Host "Installing Python" $python_version "for" $architecture "bit architecture to" $python_home
  34. if (Test-Path $python_home) {
  35. Write-Host $python_home "already exists, skipping."
  36. return $false
  37. }
  38. if ($architecture -eq "32") {
  39. $platform_suffix = ""
  40. } else {
  41. $platform_suffix = ".amd64"
  42. }
  43. $filepath = DownloadPython $python_version $platform_suffix
  44. Write-Host "Installing" $filepath "to" $python_home
  45. $args = "/qn /i $filepath TARGETDIR=$python_home"
  46. Write-Host "msiexec.exe" $args
  47. Start-Process -FilePath "msiexec.exe" -ArgumentList $args -Wait -Passthru
  48. Write-Host "Python $python_version ($architecture) installation complete"
  49. return $true
  50. }
  51. function InstallPip ($python_home) {
  52. $pip_path = $python_home + "/Scripts/pip.exe"
  53. $python_path = $python_home + "/python.exe"
  54. if (-not(Test-Path $pip_path)) {
  55. Write-Host "Installing pip..."
  56. $webclient = New-Object System.Net.WebClient
  57. $webclient.DownloadFile($GET_PIP_URL, $GET_PIP_PATH)
  58. Write-Host "Executing:" $python_path $GET_PIP_PATH
  59. Start-Process -FilePath "$python_path" -ArgumentList "$GET_PIP_PATH" -Wait -Passthru
  60. } else {
  61. Write-Host "pip already installed."
  62. }
  63. }
  64. function InstallPackage ($python_home, $pkg) {
  65. $pip_path = $python_home + "/Scripts/pip.exe"
  66. & $pip_path install $pkg
  67. }
  68. function main () {
  69. InstallPython $env:PYTHON_VERSION $env:PYTHON_ARCH $env:PYTHON
  70. InstallPip $env:PYTHON
  71. InstallPackage $env:PYTHON wheel
  72. }
  73. main