Skip to content

Instantly share code, notes, and snippets.

@eliziario
Forked from carlosfunk/install_python.ps1
Last active October 25, 2017 17:49
Show Gist options
  • Save eliziario/053fdba7f6cdfd5d2bea683bd4b2720c to your computer and use it in GitHub Desktop.
Save eliziario/053fdba7f6cdfd5d2bea683bd4b2720c to your computer and use it in GitHub Desktop.
Powershell scripts for setting up a Python environment under Windows
$save_dir=Resolve-Path ~/Downloads
$project_dir = "C:\Projects"
$virtualenv_dir = $project_dir + "\virtualenvs"
$client = New-Object System.Net.WebClient
function InstallPythonExe($installer) {
$Arguments = @()
$Arguments += "InstallAllUsers=`"1`""
$Arguments += "DefaultCustomTargetDir=C:\\Python"
$Arguments += "TargetDir=C:\\Python"
$Arguments += "/passive"
Start-Process "`"$installer`"" -ArgumentList $Arguments -Wait
}
function download-file([string]$url, [string]$d) {
# Downloads a file if it doesn't already exist
if(!(Test-Path $d -pathType leaf)) {
# get the file
write-host "Downloading $url to $d";
$client.DownloadFile($url, $d);
}
}
function install-python-ver($version) {
[Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Python")
$curversion = Get-ChildItem -Path Registry::HKLM\Software\Python\PythonCore\ -ErrorAction "SilentlyContinue"
if ($curversion) {
if ($curversion[0].GetValue("Version") -eq "3.6.2" ) {
write-host "Version Already Installed, quiting"
}
}
# Download Python indicated by version. For example:
# > install-python-ver 3.4.0rc1
# or
# > install-python-ver 2.7.6
$filename = 'python-' + $version + '-amd64.exe';
$save_path = '' + $save_dir + '\' + $filename;
if(!(Test-Path -pathType container $save_dir)) {
write-host -fore red $save_dir " does not exist";
exit;
}
$url = 'http://www.python.org/ftp/python/' + $version.Substring(0,5) + '/' + $filename;
download-file $url $save_path
write-host "Installing Python $url"
InstallPythonExe $save_path $target_dir
write-host "Add Python to the PATH"
[Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Python27\;C:\Python27\Scripts\", "User")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment