Инструменты пользователя

Инструменты сайта


windows:portable_tools:python:start

Это старая версия документа!


Python

Python Embeded

необходимо на чистую
Python
Lib\site-packages\pip\
Lib\site-packages\setuptools\
Lib\site-packages\wheel\
Scripts\\

# =========================================
# File: python_env_repair.ps1
# =========================================
# DESCRIPTION:
#   Portable Python Embeddable
#   Bootstrap / Repair Tool
#
# FEATURES:
#   - Verifies portable Python structure
#   - Enables "import site"
#   - Creates required folders
#   - Creates isolated launcher
#   - Verifies user-site isolation
#   - Displays sys.path
#
# NOTES:
#   Python 3.14+ embeddable no longer
#   includes ensurepip by default.
#
#   pip may need to be copied manually
#   from another Python installation.
#
# USAGE:
#
#   Default:
#   powershell -ExecutionPolicy Bypass -File .\python_env_repair.ps1
#
#   Custom path:
#   powershell -ExecutionPolicy Bypass -File .\python_env_repair.ps1 "D:\PortablePython"
#
# =========================================
 
# -----------------------------------------
# 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"    }
        "INFO"  { $Color = "Cyan"   }
 
        default { $Color = "White"  }
    }
 
    Write-Host ("[{0}]" -f $Status).PadRight(10) -ForegroundColor $Color -NoNewline
    Write-Host $Message
}
 
# -----------------------------------------
# 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"
 
# -----------------------------------------
# Header
# -----------------------------------------
 
Write-Host ""
Write-Host "=========================================" -ForegroundColor Cyan
Write-Host " Portable Python Repair Tool"            -ForegroundColor Cyan
Write-Host "=========================================" -ForegroundColor Cyan
Write-Host ""
 
Write-Host "Python Root:" -ForegroundColor Cyan
Write-Host $RootDir
Write-Host ""
 
# -----------------------------------------
# Verify python.exe
# -----------------------------------------
 
if (-not (Test-Path $PythonExe)) {
 
    Write-Status "ERROR" "python.exe NOT found"
 
    Write-Host ""
    Read-Host "Press Enter"
 
    exit
}
 
Write-Status "OK" "python.exe found"
 
# -----------------------------------------
# Find pythonXY._pth
# -----------------------------------------
 
$PthFile = Get-ChildItem `
    -Path $RootDir `
    -Filter "python*._pth" `
    -ErrorAction SilentlyContinue |
    Select-Object -First 1
 
if (-not $PthFile) {
 
    Write-Status "ERROR" "pythonXY._pth NOT found"
 
    Write-Host ""
    Read-Host "Press Enter"
 
    exit
}
 
Write-Status "OK" "$($PthFile.Name) found"
 
# -----------------------------------------
# Enable import site
# -----------------------------------------
 
$PthContent = Get-Content $PthFile.FullName
 
$ImportSiteEnabled = $false
 
for ($i = 0; $i -lt $PthContent.Count; $i++) {
 
    $Line = $PthContent[$i].Trim()
 
    if ($Line -eq "import site") {
 
        $ImportSiteEnabled = $true
    }
 
    if ($Line -eq "#import site") {
 
        $PthContent[$i] = "import site"
 
        $ImportSiteEnabled = $true
 
        Write-Status "INFO" "Enabled import site"
    }
}
 
if (-not $ImportSiteEnabled) {
 
    $PthContent += "import site"
 
    Write-Status "INFO" "Added import site"
}
 
Set-Content `
    -Path $PthFile.FullName `
    -Value $PthContent `
    -Encoding UTF8
 
Write-Status "OK" "import site enabled"
 
# -----------------------------------------
# Create Required Folders
# -----------------------------------------
 
$RequiredFolders = @(
    "Lib",
    "Lib\site-packages",
    "Scripts",
    "TEMP",
    "CACHE",
    "LOGS",
    "TOOLS"
)
 
foreach ($Folder in $RequiredFolders) {
 
    $FullPath = Join-Path $RootDir $Folder
 
    if (-not (Test-Path $FullPath)) {
 
        New-Item `
            -ItemType Directory `
            -Path $FullPath `
            -Force | Out-Null
 
        Write-Status "INFO" "Created: $Folder"
    }
    else {
 
        Write-Status "OK" "Exists: $Folder"
    }
}
 
# -----------------------------------------
# Check pip
# -----------------------------------------
 
$PipInstalled = $false
 
try {
 
    & $PythonExe -m pip --version *> $null
 
    if ($LASTEXITCODE -eq 0) {
 
        $PipInstalled = $true
    }
}
catch {}
 
if ($PipInstalled) {
 
    Write-Status "OK" "pip available"
}
else {
 
    Write-Status "WARN" "pip NOT available"
 
    Write-Status "INFO" "Python 3.14+ embeddable may not include ensurepip"
 
    Write-Status "INFO" "Copy pip manually from another Python installation if needed"
}
 
# -----------------------------------------
# Create py_portable.cmd
# -----------------------------------------
 
$LauncherPath = Join-Path $RootDir "py_portable.cmd"
 
$LauncherContent = @'
:: ========================================
:: File: py_portable.cmd
:: ========================================
 
@echo off
chcp 65001 >nul
 
setlocal
 
:: ----------------------------------------
:: Portable Python Root
:: ----------------------------------------
 
set "PYROOT=%~dp0"
 
:: ----------------------------------------
:: Isolated Environment
:: ----------------------------------------
 
set "PYTHONHOME="
set "PYTHONPATH="
set "PYTHONNOUSERSITE=1"
 
:: Optional:
:: set "PYTHONDONTWRITEBYTECODE=1"
 
set "PATH=%PYROOT%Scripts;%PATH%"
 
:: ----------------------------------------
:: Interactive Mode
:: ----------------------------------------
 
if "%~1"=="" (
 
    "%PYROOT%python.exe"
 
    endlocal
    exit /b
)
 
:: ----------------------------------------
:: Python -c Support
:: ----------------------------------------
 
if "%~1"=="-c" (
 
    "%PYROOT%python.exe" %*
 
    endlocal
    exit /b
)
 
:: ----------------------------------------
:: Python -m Support
:: ----------------------------------------
 
if "%~1"=="-m" (
 
    "%PYROOT%python.exe" %*
 
    endlocal
    exit /b
)
:: ----------------------------------------
:: Script Mode
:: ----------------------------------------
 
set "SCRIPT=%~1"
shift
 
pushd "%~dp1"
 
"%PYROOT%python.exe" "%SCRIPT%" %*
 
popd
 
endlocal
'@
 
Set-Content `
    -Path $LauncherPath `
    -Value $LauncherContent `
    -Encoding ASCII
 
Write-Status "OK" "Created py_portable.cmd"
 
# -----------------------------------------
# Verify user-site isolation
# -----------------------------------------
 
Write-Status "INFO" "Checking user-site isolation..."
 
try {
 
    $UserSite = & $LauncherPath -c "import site; print(site.ENABLE_USER_SITE)"
 
    if ($UserSite -match "False") {
 
        Write-Status "OK" "user-site disabled"
    }
    else {
 
        Write-Status "WARN" "user-site still enabled"
    }
}
catch {
 
    Write-Status "WARN" "Cannot verify user-site"
}
 
# -----------------------------------------
# Show user-site path
# -----------------------------------------
 
try {
 
    $UserSitePath = & $LauncherPath -c "import site; print(site.getusersitepackages())"
 
    Write-Host ""
    Write-Host "User Site Path:" -ForegroundColor Cyan
    Write-Host $UserSitePath
}
catch {}
 
# -----------------------------------------
# Show sys.path
# -----------------------------------------
 
Write-Host ""
Write-Host "sys.path:" -ForegroundColor Cyan
 
try {
 
    & $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 " Repair Complete"                        -ForegroundColor Cyan
Write-Host "=========================================" -ForegroundColor Cyan
Write-Host ""
 
Read-Host "Press Enter"
windows/portable_tools/python/start.1778749358.txt.gz · Последнее изменение:

Если не указано иное, содержимое этой вики предоставляется на условиях следующей лицензии: CC Attribution 4.0 International
CC Attribution 4.0 International Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki