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
+
8
+
9
+ function DownloadMiniconda ($python_version , $platform_suffix ) {
10
+ $webclient = New-Object System.Net.WebClient
11
+ if ($python_version -eq " 3.4" ) {
12
+ $filename = " Miniconda3-3.7.3-Windows-" + $platform_suffix + " .exe"
13
+ } else {
14
+ $filename = " Miniconda-3.7.3-Windows-" + $platform_suffix + " .exe"
15
+ }
16
+ $url = $MINICONDA_URL + $filename
17
+
18
+ $basedir = $pwd.Path + " \"
19
+ $filepath = $basedir + $filename
20
+ if (Test-Path $filename ) {
21
+ Write-Host " Reusing" $filepath
22
+ return $filepath
23
+ }
24
+
25
+ # Download and retry up to 3 times in case of network transient errors.
26
+ Write-Host " Downloading" $filename " from" $url
27
+ $retry_attempts = 2
28
+ for ($i = 0 ; $i -lt $retry_attempts ; $i ++ ){
29
+ try {
30
+ $webclient.DownloadFile ($url , $filepath )
31
+ break
32
+ }
33
+ Catch [Exception ]{
34
+ Start-Sleep 1
35
+ }
36
+ }
37
+ if (Test-Path $filepath ) {
38
+ Write-Host " File saved at" $filepath
39
+ } else {
40
+ # Retry once to get the error message if any at the last try
41
+ $webclient.DownloadFile ($url , $filepath )
42
+ }
43
+ return $filepath
44
+ }
45
+
46
+
47
+ function InstallMiniconda ($python_version , $architecture , $python_home ) {
48
+ Write-Host " Installing Python" $python_version " for" $architecture " bit architecture to" $python_home
49
+ if (Test-Path $python_home ) {
50
+ Write-Host $python_home " already exists, skipping."
51
+ return $false
52
+ }
53
+ if ($architecture -eq " 32" ) {
54
+ $platform_suffix = " x86"
55
+ } else {
56
+ $platform_suffix = " x86_64"
57
+ }
58
+ $filepath = DownloadMiniconda $python_version $platform_suffix
59
+ Write-Host " Installing" $filepath " to" $python_home
60
+ $install_log = $python_home + " .log"
61
+ $args = " /S /D=$python_home "
62
+ Write-Host $filepath $args
63
+ Start-Process - FilePath $filepath - ArgumentList $args - Wait - Passthru
64
+ if (Test-Path $python_home ) {
65
+ Write-Host " Python $python_version ($architecture ) installation complete"
66
+ } else {
67
+ Write-Host " Failed to install Python in $python_home "
68
+ Get-Content - Path $install_log
69
+ Exit 1
70
+ }
71
+ }
72
+
73
+
74
+ function InstallMinicondaPip ($python_home ) {
75
+ $pip_path = $python_home + " \Scripts\pip.exe"
76
+ $conda_path = $python_home + " \Scripts\conda.exe"
77
+ if (-not (Test-Path $pip_path )) {
78
+ Write-Host " Installing pip..."
79
+ $args = " install --yes pip"
80
+ Write-Host $conda_path $args
81
+ Start-Process - FilePath " $conda_path " - ArgumentList $args - Wait - Passthru
82
+ } else {
83
+ Write-Host " pip already installed."
84
+ }
85
+ }
86
+
87
+
88
+ function main () {
89
+ InstallMiniconda $env: PYTHON_VERSION $env: PYTHON_ARCH $env: PYTHON
90
+ InstallMinicondaPip $env: PYTHON
91
+ }
92
+
93
+ main
0 commit comments