-
Notifications
You must be signed in to change notification settings - Fork 1
/
Setup-Python.ps1
61 lines (50 loc) · 1.8 KB
/
Setup-Python.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
######
#
# Setup-Python.ps1
#
#
###
function Reload-Path {
# Updates current session PATH reading the most updated one from Registry
$env:Path = (Get-ItemProperty 'HKLM:\System\CurrentControlSet\Control\Session Manager\Environment').Path + ';' + `
(Get-ItemProperty 'HKCU:\Environment').Path
}
# Source: https://stackoverflow.com/a/34844707
function Add-EnvPath {
param(
[Parameter(Mandatory=$true)]
[string] $Path,
[ValidateSet('Machine', 'User', 'Session')]
[string] $Container = 'Session'
)
if ($Container -ne 'Session') {
$containerMapping = @{
Machine = [EnvironmentVariableTarget]::Machine
User = [EnvironmentVariableTarget]::User
}
$containerType = $containerMapping[$Container]
$persistedPaths = [Environment]::GetEnvironmentVariable('Path', $containerType) -split ';'
if ($persistedPaths -notcontains $Path) {
$persistedPaths = $persistedPaths + $Path | where { $_ }
[Environment]::SetEnvironmentVariable('Path', $persistedPaths -join ';', $containerType)
}
}
$envPaths = $env:Path -split ';'
if ($envPaths -notcontains $Path) {
$envPaths = $envPaths + $Path | where { $_ }
$env:Path = $envPaths -join ';'
}
}
# Stop on any error
$ErrorActionPreference = 'Stop'
# Install Python
choco install python -y
Reload-Path
# Update Pip
python.exe -m pip install --upgrade pip
# Install Poetry (unofficial, disables SSL certificate validation)
(Invoke-WebRequest -Uri https://raw.githubusercontent.com/fdcastel/install-poetry/main/install-poetry.py -UseBasicParsing).Content | python.exe -
Add-EnvPath -Path "$env:APPDATA\Python\Scripts" -Container User
Reload-Path
poetry config virtualenvs.in-project true