# ========================================= # File: python_env_check.ps1 # ========================================= # DESCRIPTION: # Portable Python Embeddable Environment Checker # # FEATURES: # - Checks: # python.exe # pythonXY._pth # import site # pip # site-packages # writable TEMP # PYTHONHOME # PYTHONPATH # user-site # # - Displays: # Python version # Executable path # Pip version # Site-packages path # # USAGE: # # powershell -ExecutionPolicy Bypass -File .\python_env_check.ps1 # # NOTES: # Script must be placed INSIDE portable Python root directory. # # EXAMPLE: # # PortablePython\ # ├── python.exe # ├── python312._pth # ├── python_env_check.ps1 # └── Lib\ # # ========================================= # ----------------------------------------- # UTF-8 Console # ----------------------------------------- [Console]::OutputEncoding = [System.Text.UTF8Encoding]::new() # ----------------------------------------- # Helper Functions # ----------------------------------------- function Write-Status { param ( [string]$Status, [string]$Message ) switch ($Status) { "OK" { $color = "Green" } "WARN" { $color = "Yellow" } "ERROR" { $color = "Red" } default { $color = "White" } } Write-Host ("[{0}]" -f $Status).PadRight(10) -ForegroundColor $color -NoNewline Write-Host $Message } function Test-WritableDirectory { param ( [string]$Path ) try { $testFile = Join-Path $Path "write_test.tmp" "TEST" | Out-File -FilePath $testFile -Encoding utf8 -Force if (Test-Path $testFile) { Remove-Item $testFile -Force -ErrorAction SilentlyContinue return $true } return $false } catch { return $false } } # ----------------------------------------- # Root Paths # ----------------------------------------- if ($args.Count -ge 1) { $RootDir = $args[0] } else { $ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path $RootDir = Join-Path $ScriptDir "Python" } $RootDir = [System.IO.Path]::GetFullPath($RootDir) $PythonExe = Join-Path $RootDir "python.exe" $LauncherPath = Join-Path $RootDir "py_portable.cmd" Write-Host "" Write-Host "Python Root:" -ForegroundColor Cyan Write-Host $RootDir Write-Host "" # ----------------------------------------- # Header # ----------------------------------------- Write-Host "" Write-Host "=========================================" -ForegroundColor Cyan Write-Host " Portable Python Environment Checker" -ForegroundColor Cyan Write-Host "=========================================" -ForegroundColor Cyan Write-Host "" # ----------------------------------------- # Check python.exe # ----------------------------------------- if (Test-Path $PythonExe) { Write-Status "OK" "python.exe found" } else { Write-Status "ERROR" "python.exe NOT found" Write-Host "" Read-Host "Press Enter" exit } # ----------------------------------------- # Find pythonXY._pth # ----------------------------------------- $PthFile = Get-ChildItem -Path $RootDir -Filter "python*._pth" -ErrorAction SilentlyContinue | Select-Object -First 1 if ($PthFile) { Write-Status "OK" "$($PthFile.Name) found" } else { Write-Status "ERROR" "pythonXY._pth NOT found" } # ----------------------------------------- # Check import site # ----------------------------------------- if ($PthFile) { $PthContent = Get-Content $PthFile.FullName -ErrorAction SilentlyContinue $ImportSiteEnabled = $false foreach ($line in $PthContent) { $trim = $line.Trim() if ($trim -eq "import site") { $ImportSiteEnabled = $true } } if ($ImportSiteEnabled) { Write-Status "OK" "import site enabled" } else { Write-Status "WARN" "import site DISABLED" } } # ----------------------------------------- # Python Version # ----------------------------------------- try { $PythonVersion = & $LauncherPath --version 2>&1 Write-Status "OK" "$PythonVersion" } catch { Write-Status "ERROR" "Cannot execute python.exe" } # ----------------------------------------- # Executable Path # ----------------------------------------- try { $ExePath = & $LauncherPath -c "import sys; print(sys.executable)" Write-Status "OK" "Executable: $ExePath" } catch { Write-Status "ERROR" "Cannot get executable path" } # ----------------------------------------- # Check pip # ----------------------------------------- try { $PipVersion = & $LauncherPath -m pip --version 2>&1 if ($LASTEXITCODE -eq 0) { Write-Status "OK" "pip available" Write-Host " $PipVersion" } else { Write-Status "WARN" "pip NOT available" } } catch { Write-Status "WARN" "pip check failed" } # ----------------------------------------- # Check site-packages # ----------------------------------------- try { $SitePackages = & $LauncherPath -c "import site; print(site.getsitepackages()[0])" 2>&1 if ($LASTEXITCODE -eq 0) { Write-Status "OK" "site-packages available" Write-Host " $SitePackages" if (Test-Path $SitePackages) { Write-Status "OK" "site-packages path exists" } else { Write-Status "WARN" "site-packages path missing" } } else { Write-Status "WARN" "site-packages unavailable" } } catch { Write-Status "WARN" "Cannot check site-packages" } # ----------------------------------------- # Check user-site # ----------------------------------------- try { $UserSite = & $LauncherPath -c "import site; print(site.ENABLE_USER_SITE)" if ($UserSite -eq "False") { Write-Status "OK" "user-site disabled" } else { Write-Status "WARN" "user-site ENABLED" } } catch { Write-Status "WARN" "Cannot check user-site" } # ----------------------------------------- # Check User Site Path # ----------------------------------------- try { $UserSitePath = & $LauncherPath -c "import site; print(site.getusersitepackages())" if ($UserSite -eq "False") { Write-Status "OK" "User site isolated" Write-Host " $UserSitePath" } else { Write-Status "WARN" "User site ENABLED" Write-Host " $UserSitePath" } } catch { Write-Status "WARN" "Cannot check user-site path" } # ----------------------------------------- # Check PYTHONHOME # ----------------------------------------- if ($env:PYTHONHOME) { Write-Status "WARN" "PYTHONHOME detected: $($env:PYTHONHOME)" } else { Write-Status "OK" "PYTHONHOME not set" } # ----------------------------------------- # Check PYTHONPATH # ----------------------------------------- if ($env:PYTHONPATH) { Write-Status "WARN" "PYTHONPATH detected: $($env:PYTHONPATH)" } else { Write-Status "OK" "PYTHONPATH not set" } # ----------------------------------------- # Check TEMP Write Access # ----------------------------------------- $TempDir = Join-Path $RootDir "TEMP" if (-not (Test-Path $TempDir)) { try { New-Item -ItemType Directory -Path $TempDir -Force | Out-Null } catch {} } if (Test-WritableDirectory -Path $TempDir) { Write-Status "OK" "TEMP writable" } else { Write-Status "WARN" "TEMP NOT writable" } # ----------------------------------------- # Check Local site-packages Folder # ----------------------------------------- $LocalSitePackages = Join-Path $RootDir "Lib\site-packages" if (Test-Path $LocalSitePackages) { Write-Status "OK" "Local site-packages exists" } else { Write-Status "WARN" "Local site-packages missing" } # ----------------------------------------- # Check sys.path # ----------------------------------------- try { Write-Host "" Write-Host "sys.path:" -ForegroundColor Cyan & $LauncherPath -c "import sys; [print(x) for x in sys.path]" } catch { Write-Status "WARN" "Cannot display sys.path" } # ----------------------------------------- # Footer # ----------------------------------------- Write-Host "" Write-Host "=========================================" -ForegroundColor Cyan Write-Host " Check Complete" -ForegroundColor Cyan Write-Host "=========================================" -ForegroundColor Cyan Write-Host "" Read-Host "Press Enter"