Skip to content

Instantly share code, notes, and snippets.

@Teach2Breach
Created July 2, 2025 17:16
Show Gist options
  • Select an option

  • Save Teach2Breach/86d85f3e1ae4d07461939ebd4d203b1e to your computer and use it in GitHub Desktop.

Select an option

Save Teach2Breach/86d85f3e1ae4d07461939ebd4d203b1e to your computer and use it in GitHub Desktop.
run python on windows without install and with full pip support
# PowerShell script to download and install Python embeddable package with pip
# This script replicates the functionality of the download_extract_python() function
param(
[string]$PythonVersion = "3.10.11",
[string]$TargetDir = "C:\Users\Public\python-3.10.11",
[string]$PythonUrl = "https://www.python.org/ftp/python/3.10.11/python-3.10.11-embed-amd64.zip",
[string]$PipUrl = "https://bootstrap.pypa.io/get-pip.py"
)
# Function to write colored output
function Write-Status {
param([string]$Message, [string]$Color = "White")
Write-Host $Message -ForegroundColor $Color
}
# Function to download file from URL
function Download-File {
param([string]$Url, [string]$OutFile)
try {
Write-Status "Downloading $OutFile..." "Yellow"
Invoke-WebRequest -Uri $Url -OutFile $OutFile -UseBasicParsing
Write-Status "Downloaded $OutFile successfully" "Green"
return $true
}
catch {
$errorMsg = $_.Exception.Message
Write-Status "Failed to download $OutFile`: $errorMsg" "Red"
return $false
}
}
# Function to extract ZIP file
function Expand-ZipFile {
param([string]$ZipPath, [string]$ExtractPath)
try {
Write-Status "Extracting $ZipPath to $ExtractPath..." "Yellow"
Expand-Archive -Path $ZipPath -DestinationPath $ExtractPath -Force
Write-Status "Extracted successfully" "Green"
return $true
}
catch {
$errorMsg = $_.Exception.Message
Write-Status "Failed to extract $ZipPath`: $errorMsg" "Red"
return $false
}
}
# Main execution starts here
Write-Status "Starting Python embeddable package installation..." "Cyan"
# Check if Python is already installed
$PythonExePath = Join-Path $TargetDir "python.exe"
if (Test-Path $PythonExePath) {
Write-Status "Python installation already exists at $TargetDir, skipping download and extraction..." "Yellow"
} else {
Write-Status "Downloading and extracting Python embeddable package..." "Cyan"
# Download Python package only if not already present
$PythonZip = "python.zip"
if (-not (Test-Path $PythonZip)) {
if (-not (Download-File -Url $PythonUrl -OutFile $PythonZip)) {
Write-Status "Failed to download Python package. Exiting." "Red"
exit 1
}
} else {
Write-Status "Python ZIP file already exists, skipping download..." "Yellow"
}
# Extract only if target directory doesn't exist
if (-not (Test-Path $TargetDir)) {
if (-not (Expand-ZipFile -ZipPath $PythonZip -ExtractPath $TargetDir)) {
Write-Status "Failed to extract Python package. Exiting." "Red"
exit 1
}
Write-Status "Python embeddable package extracted successfully." "Green"
} else {
Write-Status "Target directory already exists, skipping extraction..." "Yellow"
}
}
# Check if pip is already installed
Write-Status "Checking if pip is already installed..." "Cyan"
try {
$pipCheck = & $PythonExePath -m pip --version 2>$null
if ($LASTEXITCODE -eq 0) {
Write-Status "pip is already installed, skipping pip installation..." "Yellow"
Write-Status "pip version: $pipCheck" "Green"
exit 0
}
} catch {
Write-Status "pip not found, proceeding with installation..." "Yellow"
}
# Download get-pip.py if not already present
$GetPipFile = "get-pip.py"
if (-not (Test-Path $GetPipFile)) {
Write-Status "Downloading get-pip.py..." "Cyan"
if (-not (Download-File -Url $PipUrl -OutFile $GetPipFile)) {
Write-Status "Failed to download get-pip.py. Exiting." "Red"
exit 1
}
} else {
Write-Status "get-pip.py already exists, skipping download..." "Yellow"
}
# Copy get-pip.py to Python installation directory
$TargetGetPip = Join-Path $TargetDir "get-pip.py"
try {
Copy-Item -Path $GetPipFile -Destination $TargetGetPip -Force
Write-Status "Copied get-pip.py to Python installation directory" "Green"
} catch {
$errorMsg = $_.Exception.Message
Write-Status "Failed to copy get-pip.py`: $errorMsg" "Red"
exit 1
}
# Execute get-pip.py
Write-Status "Installing pip..." "Cyan"
try {
$pipOutput = & $PythonExePath $TargetGetPip 2>&1
Write-Status "pip installation completed" "Green"
} catch {
$errorMsg = $_.Exception.Message
Write-Status "Failed to execute get-pip.py`: $errorMsg" "Red"
exit 1
}
# Edit python310._pth file to allow submodules
$PthFile = Join-Path $TargetDir "python310._pth"
if (Test-Path $PthFile) {
Write-Status "Patching python310._pth to enable site packages..." "Cyan"
try {
$content = Get-Content $PthFile -Raw
$patchedContent = $content -replace '#import site', 'import site'
Set-Content -Path $PthFile -Value $patchedContent -NoNewline
Write-Status "Successfully patched python310._pth" "Green"
} catch {
$errorMsg = $_.Exception.Message
Write-Status "Failed to patch python310._pth`: $errorMsg" "Red"
exit 1
}
} else {
Write-Status "Warning: python310._pth file not found" "Yellow"
}
# Verify pip installation
Write-Status "Verifying pip installation..." "Cyan"
try {
$pipVersionOutput = & $PythonExePath -m pip --version 2>&1
if ($LASTEXITCODE -eq 0) {
Write-Status "pip installed successfully" "Green"
Write-Status "pip version: $pipVersionOutput" "Green"
} else {
Write-Status "pip failed to install" "Red"
Write-Status "Error output: $pipVersionOutput" "Red"
exit 1
}
} catch {
$errorMsg = $_.Exception.Message
Write-Status "Failed to verify pip installation`: $errorMsg" "Red"
exit 1
}
Write-Status "Python embeddable package installation completed successfully!" "Green"
Write-Status "Python location: $PythonExePath" "Cyan"
Write-Status "You can now use: & '$PythonExePath' -m pip install <package>" "Cyan"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment