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

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


windows:portable_tools:python:start

Различия

Показаны различия между двумя версиями страницы.

Ссылка на это сравнение

Предыдущая версия справа и слеваПредыдущая версия
Следующая версия
Предыдущая версия
windows:portable_tools:python:start [2026/05/14 12:01] windows:portable_tools:python:start [2026/05/14 12:06] (текущий)
Строка 1: Строка 1:
 ====== Python ====== ====== Python ======
-Python Embeded\\+===== Python Embeded =====
 необходимо на чистую\\ необходимо на чистую\\
 Python\\ Python\\
Строка 7: Строка 7:
 ''Lib\site-packages\wheel\''\\ ''Lib\site-packages\wheel\''\\
 ''Scripts\\'' ''Scripts\\''
 +
 +==== Проверка на соответствие ====
 +
 +<code powershell python_env_check.ps1>
 +# =========================================
 +# 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"
 +</code>
 +
 +==== Исправление ====
 +
 +<code powershell python_env_repair.ps1>
 +# =========================================
 +# 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"
 +</code>
windows/portable_tools/python/start.1778749295.txt.gz · Последнее изменение:

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