Last active
November 2, 2022 08:27
-
-
Save parched/9b999e0dfd0b699f7f0fcfc1ccb7cecd to your computer and use it in GitHub Desktop.
Set up a directory for Jupyter notebook development with VS Code (Windows only)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Set up the current directory for Jupyter notebooks in VS Code | |
# Windows 64-bit only | |
Write-Host "Installing required tools for current user" | |
$Downloads = (New-Object -ComObject Shell.Application).NameSpace('shell:Downloads').Self.Path | |
Get-Command -ErrorAction SilentlyContinue py | Out-Null | |
if ($?) { py -3.10 --version 2>&1> $null } | |
if (-Not $?) { | |
$PythonInstallerUrl = 'https://www.python.org/ftp/python/3.10.8/python-3.10.8-amd64.exe' | |
$PythonInstaller = "$Downloads\python-3.10.8-amd64.exe" | |
if (-Not (Test-Path $PythonInstaller)) { | |
Write-Host "Downloading Python 3.10.8" | |
Invoke-WebRequest -Uri $PythonInstallerUrl -OutFile $PythonInstaller | |
} | |
Write-Host "Installing Python 3.10.8" | |
Start-Process -Wait $PythonInstaller -ArgumentList '/passive InstallAllUsers=0 InstallLauncherAllUsers=0 Include_launcher=1' | |
if (-Not $?) { throw "Failed to install Python: $LASTEXITCODE" } | |
# Make sure the new launcher is in the path | |
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User") | |
} else { | |
Write-Host "Python 3.10.8 is already installed" | |
} | |
$VsCodeExePath = "${env:LocalAppData}\Programs\Microsoft VS Code\bin\code.cmd" | |
$VsCodeInstallerUrl = "https://update.code.visualstudio.com/latest/win32-x64-user/stable" | |
if (-Not (Test-Path $VsCodeExePath)) { | |
$VsCodeInstallerPath = "$Downloads\vscode-install.exe" | |
if (-Not (Test-Path $VsCodeInstallerPath)) { | |
Write-Host "Downloading VS Code" | |
Invoke-WebRequest -Uri $VsCodeInstallerUrl -OutFile $VsCodeInstallerPath | |
} | |
Write-Host "Installing VS Code" | |
Start-Process -Wait $VsCodeInstallerPath -ArgumentList '/verysilent /tasks=addcontextmenufiles,addcontextmenufolders,addtopath' | |
} else { | |
Write-Host "VS Code is already installed" | |
} | |
Write-Host "Installing Python extension" | |
& $VsCodeExePath --install-extension "ms-python.python" | |
Write-Host "Setting up current directory" | |
if (-Not (Test-Path ".venv")) { | |
Write-Host "Creating virtual environment" | |
py -3.10 -m venv .venv | |
} | |
Write-Host "Activating virtual environment" | |
.venv\Scripts\Activate.ps1 | |
Write-Host "Installing Jupyter requirements" | |
python -m pip install ipykernel pandas scipy matplotlib | |
if (-Not (Test-Path ".vscode\settings.json")) { | |
New-Item -ItemType Directory -Force -Path ".vscode" | |
Set-Content -Path ".vscode\settings.json" -Value @' | |
{ | |
"python.defaultInterpreterPath": "${workspaceFolder}/.venv/Scripts/python.exe", | |
} | |
'@ | |
} | |
Write-Host "Opening VS Code" | |
& $VsCodeExePath . |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment