install_python_interpreters.ps1 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #!/usr/bin/env powershell
  2. # Install Python 3.8 for x64 and x86 in order to build wheels on Windows.
  3. Set-StrictMode -Version 2
  4. $ErrorActionPreference = 'Stop'
  5. trap {
  6. $ErrorActionPreference = "Continue"
  7. Write-Error $_
  8. exit 1
  9. }
  10. # Avoid "Could not create SSL/TLS secure channel"
  11. [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
  12. function Install-Python {
  13. Param(
  14. [string]$PythonVersion,
  15. [string]$PythonInstaller,
  16. [string]$PythonInstallPath,
  17. [string]$PythonInstallerHash
  18. )
  19. $PythonInstallerUrl = "https://www.python.org/ftp/python/$PythonVersion/$PythonInstaller.exe"
  20. $PythonInstallerPath = "C:\tools\$PythonInstaller.exe"
  21. # Downloads installer
  22. Write-Host "Downloading the Python installer: $PythonInstallerUrl => $PythonInstallerPath"
  23. Invoke-WebRequest -Uri $PythonInstallerUrl -OutFile $PythonInstallerPath
  24. # Validates checksum
  25. $HashFromDownload = Get-FileHash -Path $PythonInstallerPath -Algorithm MD5
  26. if ($HashFromDownload.Hash -ne $PythonInstallerHash) {
  27. throw "Invalid Python installer: failed checksum!"
  28. }
  29. Write-Host "Python installer $PythonInstallerPath validated."
  30. # Installs Python
  31. & $PythonInstallerPath /passive InstallAllUsers=1 PrependPath=1 Include_test=0 TargetDir=$PythonInstallPath
  32. if (-Not $?) {
  33. throw "The Python installation exited with error!"
  34. }
  35. # NOTE(lidiz) Even if the install command finishes in the script, that
  36. # doesn't mean the Python installation is finished. If using "ps" to check
  37. # for running processes, you might see ongoing installers at this point.
  38. # So, we needs this "hack" to reliably validate that the Python binary is
  39. # functioning properly.
  40. # Wait for the installer process
  41. Wait-Process -Name $PythonInstaller -Timeout 300
  42. Write-Host "Installation process exits normally."
  43. # Validate Python binary
  44. $PythonBinary = "$PythonInstallPath\python.exe"
  45. & $PythonBinary -c 'print(42)'
  46. Write-Host "Python binary works properly."
  47. # Installs pip
  48. & $PythonBinary -m ensurepip --user
  49. Write-Host "Python $PythonVersion installed by $PythonInstaller at $PythonInstallPath."
  50. }
  51. # Python 3.8
  52. $Python38x86Config = @{
  53. PythonVersion = "3.8.0"
  54. PythonInstaller = "python-3.8.0"
  55. PythonInstallPath = "C:\Python38_32bit"
  56. PythonInstallerHash = "412a649d36626d33b8ca5593cf18318c"
  57. }
  58. Install-Python @Python38x86Config
  59. $Python38x64Config = @{
  60. PythonVersion = "3.8.0"
  61. PythonInstaller = "python-3.8.0-amd64"
  62. PythonInstallPath = "C:\Python38"
  63. PythonInstallerHash = "29ea87f24c32f5e924b7d63f8a08ee8d"
  64. }
  65. Install-Python @Python38x64Config
  66. # Python 3.9
  67. $Python39x86Config = @{
  68. PythonVersion = "3.9.0"
  69. PythonInstaller = "python-3.9.0"
  70. PythonInstallPath = "C:\Python39_32bit"
  71. PythonInstallerHash = "4a2812db8ab9f2e522c96c7728cfcccb"
  72. }
  73. Install-Python @Python39x86Config
  74. $Python39x64Config = @{
  75. PythonVersion = "3.9.0"
  76. PythonInstaller = "python-3.9.0-amd64"
  77. PythonInstallPath = "C:\Python39"
  78. PythonInstallerHash = "b61a33dc28f13b561452f3089c87eb63"
  79. }
  80. Install-Python @Python39x64Config
  81. # Python 3.10
  82. $Python310x86Config = @{
  83. PythonVersion = "3.10.0"
  84. PythonInstaller = "python-3.10.0rc1"
  85. PythonInstallPath = "C:\Python310_32bit"
  86. PythonInstallerHash = "6de353f2f7422aa030d4ccc788ffa75e"
  87. }
  88. Install-Python @Python310x86Config
  89. $Python310x64Config = @{
  90. PythonVersion = "3.10.0"
  91. PythonInstaller = "python-3.10.0rc1-amd64"
  92. PythonInstallPath = "C:\Python310"
  93. PythonInstallerHash = "39135519b044757f0a3b09d63612b0da"
  94. }
  95. Install-Python @Python310x64Config