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

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


windows:portable_tools:python:start

Различия

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

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

Предыдущая версия справа и слеваПредыдущая версия
Следующая версия
Предыдущая версия
windows:portable_tools:python:start [2026/05/14 12:03] windows:portable_tools:python:start [2026/05/14 12:06] (текущий)
Строка 8: Строка 8:
 ''Scripts\\'' ''Scripts\\''
  
-<code powershell>+==== Проверка на соответствие ==== 
 + 
 +<code powershell python_env_check.ps1>
 # ========================================= # =========================================
-# File: python_env_repair.ps1+# File: python_env_check.ps1
 # ========================================= # =========================================
 # DESCRIPTION: # DESCRIPTION:
-#   Portable Python Embeddable +#   Portable Python Embeddable Environment Checker
-#   Bootstrap / Repair Tool+
 # #
 # FEATURES: # FEATURES:
-#   Verifies portable Python structure +#   Checks: 
-  - Enables "import site" +      python.exe 
-  - Creates required folders +#       pythonXY._pth 
-  Creates isolated launcher +#       import site 
-  - Verifies user-site isolation +      pip 
-  Displays sys.path+      site-packages 
 +      writable TEMP 
 +      PYTHONHOME 
 +#       PYTHONPATH 
 +#       user-site
 # #
-NOTES+  - Displays
-  Python 3.14+ embeddable no longer +      Python version 
-  includes ensurepip by default. +      Executable path 
-+      Pip version 
-  pip may need to be copied manually +      Site-packages path
-#   from another Python installation.+
 # #
 # USAGE: # USAGE:
 # #
-#   Default: +#   powershell -ExecutionPolicy Bypass -File .\python_env_check.ps1
-#   powershell -ExecutionPolicy Bypass -File .\python_env_repair.ps1+
 # #
-  Custom path+NOTES
-#   powershell -ExecutionPolicy Bypass -File .\python_env_repair.ps1 "D:\PortablePython"+#   Script must be placed INSIDE portable Python root directory. 
 +
 +# EXAMPLE: 
 +
 +#   PortablePython\ 
 +#   ├── python.exe 
 +#   ├── python312._pth 
 +#   ├── python_env_check.ps1 
 +#   └── Lib\
 # #
 # ========================================= # =========================================
Строка 52: Строка 63:
  
 function Write-Status { function Write-Status {
- 
     param (     param (
         [string]$Status,         [string]$Status,
Строка 59: Строка 69:
  
     switch ($Status) {     switch ($Status) {
- +        "OK"    { $color = "Green" 
-        "OK"    { $Color = "Green" +        "WARN"  { $color = "Yellow"
-        "WARN"  { $Color = "Yellow"+        "ERROR" { $color = "Red"    } 
-        "ERROR" { $Color = "Red"    } +        default { $color = "White"  }
-        "INFO"  { $Color = "Cyan"   } +
- +
-        default { $Color = "White"  }+
     }     }
  
-    Write-Host ("[{0}]" -f $Status).PadRight(10) -ForegroundColor $Color -NoNewline+    Write-Host ("[{0}]" -f $Status).PadRight(10) -ForegroundColor $color -NoNewline
     Write-Host $Message     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
 +    }
 } }
  
Строка 90: Строка 119:
  
 $PythonExe = Join-Path $RootDir "python.exe" $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 ""
  
 # ----------------------------------------- # -----------------------------------------
Строка 97: Строка 132:
 Write-Host "" Write-Host ""
 Write-Host "=========================================" -ForegroundColor Cyan Write-Host "=========================================" -ForegroundColor Cyan
-Write-Host " Portable Python Repair Tool           -ForegroundColor Cyan+Write-Host " Portable Python Environment Checker   -ForegroundColor Cyan
 Write-Host "=========================================" -ForegroundColor Cyan Write-Host "=========================================" -ForegroundColor Cyan
-Write-Host "" 
- 
-Write-Host "Python Root:" -ForegroundColor Cyan 
-Write-Host $RootDir 
 Write-Host "" Write-Host ""
  
 # ----------------------------------------- # -----------------------------------------
-Verify python.exe+Check python.exe
 # ----------------------------------------- # -----------------------------------------
  
-if (-not (Test-Path $PythonExe)) { +if (Test-Path $PythonExe) { 
 +    Write-Status "OK" "python.exe found" 
 +
 +else {
     Write-Status "ERROR" "python.exe NOT found"     Write-Status "ERROR" "python.exe NOT found"
  
     Write-Host ""     Write-Host ""
     Read-Host "Press Enter"     Read-Host "Press Enter"
- 
     exit     exit
 } }
- 
-Write-Status "OK" "python.exe found" 
  
 # ----------------------------------------- # -----------------------------------------
Строка 125: Строка 155:
 # ----------------------------------------- # -----------------------------------------
  
-$PthFile = Get-ChildItem +$PthFile = Get-ChildItem -Path $RootDir -Filter "python*._pth" -ErrorAction SilentlyContinue | Select-Object -First 1
-    -Path $RootDir +
-    -Filter "python*._pth" +
-    -ErrorAction SilentlyContinue | +
-    Select-Object -First 1 +
- +
-if (-not $PthFile) {+
  
 +if ($PthFile) {
 +    Write-Status "OK" "$($PthFile.Name) found"
 +}
 +else {
     Write-Status "ERROR" "pythonXY._pth NOT found"     Write-Status "ERROR" "pythonXY._pth NOT found"
- 
-    Write-Host "" 
-    Read-Host "Press Enter" 
- 
-    exit 
 } }
- 
-Write-Status "OK" "$($PthFile.Name) found" 
  
 # ----------------------------------------- # -----------------------------------------
-Enable import site+Check import site
 # ----------------------------------------- # -----------------------------------------
  
-$PthContent = Get-Content $PthFile.FullName+if ($PthFile) {
  
-$ImportSiteEnabled = $false+    $PthContent Get-Content $PthFile.FullName -ErrorAction SilentlyContinue
  
-for ($0; $i -lt $PthContent.Count; $i++) {+    $ImportSiteEnabled = $false
  
-    $Line = $PthContent[$i].Trim()+    foreach ($line in $PthContent) {
  
-    if ($Line -eq "import site"{+        $trim = $line.Trim()
  
-        $ImportSiteEnabled = $true+        if ($trim -eq "import site") { 
 +            $ImportSiteEnabled = $true 
 +        }
     }     }
  
-    if ($Line -eq "#import site") { +    if ($ImportSiteEnabled) { 
- +        Write-Status "OK" "import site enabled
-        $PthContent[$i] = "import site" +    } 
- +    else { 
-        $ImportSiteEnabled = $true +        Write-Status "WARN" "import site DISABLED"
- +
-        Write-Status "INFO" "Enabled import site"+
     }     }
 } }
  
-if (-not $ImportSiteEnabled) {+# ----------------------------------------- 
 +# Python Version 
 +# -----------------------------------------
  
-    $PthContent +"import site"+try { 
 + 
 +    $PythonVersion & $LauncherPath --version 2>&1
  
-    Write-Status "INFO" "Added import site"+    Write-Status "OK" "$PythonVersion" 
 +
 +catch { 
 +    Write-Status "ERROR" "Cannot execute python.exe"
 } }
- 
-Set-Content ` 
-    -Path $PthFile.FullName ` 
-    -Value $PthContent ` 
-    -Encoding UTF8 
- 
-Write-Status "OK" "import site enabled" 
  
 # ----------------------------------------- # -----------------------------------------
-Create Required Folders+Executable Path
 # ----------------------------------------- # -----------------------------------------
  
-$RequiredFolders = @( +try {
-    "Lib", +
-    "Lib\site-packages", +
-    "Scripts", +
-    "TEMP", +
-    "CACHE", +
-    "LOGS", +
-    "TOOLS" +
-)+
  
-foreach ($Folder in $RequiredFolders{+    $ExePath = & $LauncherPath -c "import sys; print(sys.executable)"
  
-    $FullPath = Join-Path $RootDir $Folder+    Write-Status "OK" "Executable: $ExePath" 
 +
 +catch { 
 +    Write-Status "ERROR" "Cannot get executable path" 
 +}
  
-    if (-not (Test-Path $FullPath)) {+# ----------------------------------------- 
 +# Check pip 
 +# -----------------------------------------
  
-        New-Item ` +try {
-            -ItemType Directory ` +
-            -Path $FullPath ` +
-            -Force | Out-Null+
  
-        Write-Status "INFO" "Created: $Folder"+    $PipVersion = & $LauncherPath -m pip --version 2>&
 + 
 +    if ($LASTEXITCODE -eq 0) { 
 +        Write-Status "OK" "pip available" 
 +        Write-Host "         $PipVersion"
     }     }
     else {     else {
- +        Write-Status "WARN" "pip NOT available"
-        Write-Status "OK" "Exists: $Folder"+
     }     }
 +}
 +catch {
 +    Write-Status "WARN" "pip check failed"
 } }
  
 # ----------------------------------------- # -----------------------------------------
-# Check pip+# Check site-packages
 # ----------------------------------------- # -----------------------------------------
- 
-$PipInstalled = $false 
  
 try { try {
  
-    & $PythonExe -m pip --version *$null+    $SitePackages = & $LauncherPath -c "import site; print(site.getsitepackages()[0])" 2>&1
  
     if ($LASTEXITCODE -eq 0) {     if ($LASTEXITCODE -eq 0) {
  
-        $PipInstalled = $true+        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 {+catch { 
- +    Write-Status "WARN" "Cannot check site-packages"
-if ($PipInstalled) { +
- +
-    Write-Status "OK" "pip available"+
 } }
-else { 
  
-    Write-Status "WARN" "pip NOT available"+# ----------------------------------------- 
 +# Check user-site 
 +# -----------------------------------------
  
-    Write-Status "INFO" "Python 3.14+ embeddable may not include ensurepip"+try {
  
-    Write-Status "INFO" "Copy pip manually from another Python installation if needed"+    $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"
 } }
  
 # ----------------------------------------- # -----------------------------------------
-Create py_portable.cmd+Check User Site Path
 # ----------------------------------------- # -----------------------------------------
  
-$LauncherPath = Join-Path $RootDir "py_portable.cmd"+try {
  
-$LauncherContent @' +    $UserSitePath & $LauncherPath -c "import site; print(site.getusersitepackages())"
-:: ======================================== +
-:: File: py_portable.cmd +
-:: ========================================+
  
-@echo off +    if ($UserSite -eq "False") {
-chcp 65001 >nul+
  
-setlocal+        Write-Status "OK" "User site isolated" 
 +        Write-Host "         $UserSitePath" 
 +    } 
 +    else {
  
-:: ---------------------------------------- +        Write-Status "WARN" "User site ENABLED" 
-:: Portable Python Root +        Write-Host "         $UserSitePath" 
-:: ----------------------------------------+    } 
 +
 +catch {
  
-set "PYROOT=%~dp0"+    Write-Status "WARN"Cannot check user-site path" 
 +}
  
-:: ---------------------------------------- 
-:: Isolated Environment 
-:: ---------------------------------------- 
  
-set "PYTHONHOME=" +# ----------------------------------------- 
-set "PYTHONPATH=" +# Check PYTHONHOME 
-set "PYTHONNOUSERSITE=1"+# -----------------------------------------
  
-:: Optional: +if ($env:PYTHONHOME) { 
-:: set "PYTHONDONTWRITEBYTECODE=1"+    Write-Status "WARN" "PYTHONHOME detected$($env:PYTHONHOME)" 
 +
 +else { 
 +    Write-Status "OK" "PYTHONHOME not set" 
 +}
  
-set "PATH=%PYROOT%Scripts;%PATH%" +# ----------------------------------------- 
- +# Check PYTHONPATH 
-:: ---------------------------------------- +# -----------------------------------------
-:: Interactive Mode +
-:: ----------------------------------------+
  
-if "%~1"=="" ( +if ($env:PYTHONPATH) { 
- +    Write-Status "WARN" "PYTHONPATH detected: $($env:PYTHONPATH)" 
-    "%PYROOT%python.exe" +} 
- +else { 
-    endlocal +    Write-Status "OK" "PYTHONPATH not set" 
-    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+Check TEMP Write Access
 # ----------------------------------------- # -----------------------------------------
  
-Write-Status "INFO" "Checking user-site isolation..."+$TempDir = Join-Path $RootDir "TEMP"
  
-try {+if (-not (Test-Path $TempDir)) {
  
-    $UserSite = & $LauncherPath -c "import site; print(site.ENABLE_USER_SITE)" +    try 
- +        New-Item -ItemType Directory -Path $TempDir -Force | Out-Null
-    if ($UserSite -match "False"{ +
- +
-        Write-Status "OK" "user-site disabled" +
-    } +
-    else { +
- +
-        Write-Status "WARN" "user-site still enabled"+
     }     }
 +    catch {}
 } }
-catch { 
  
-    Write-Status "WARN" "Cannot verify user-site"+if (Test-WritableDirectory -Path $TempDir) { 
 +    Write-Status "OK" "TEMP writable" 
 +
 +else { 
 +    Write-Status "WARN" "TEMP NOT writable"
 } }
  
 # ----------------------------------------- # -----------------------------------------
-Show user-site path+Check Local site-packages Folder
 # ----------------------------------------- # -----------------------------------------
  
-try {+$LocalSitePackages = Join-Path $RootDir "Lib\site-packages"
  
-    $UserSitePath = & $LauncherPath -c "import site; print(site.getusersitepackages())" +if (Test-Path $LocalSitePackages{ 
- +    Write-Status "OK"Local site-packages exists"
-    Write-Host "" +
-    Write-Host "User Site Path:" -ForegroundColor Cyan +
-    Write-Host $UserSitePath+
 } }
-catch {}+else { 
 +    Write-Status "WARN" "Local site-packages missing" 
 +} 
  
 # ----------------------------------------- # -----------------------------------------
-Show sys.path+Check sys.path
 # ----------------------------------------- # -----------------------------------------
- 
-Write-Host "" 
-Write-Host "sys.path:" -ForegroundColor Cyan 
  
 try { try {
 +
 +    Write-Host ""
 +    Write-Host "sys.path:" -ForegroundColor Cyan
  
     & $LauncherPath -c "import sys; [print(x) for x in sys.path]"     & $LauncherPath -c "import sys; [print(x) for x in sys.path]"
Строка 393: Строка 383:
     Write-Status "WARN" "Cannot display sys.path"     Write-Status "WARN" "Cannot display sys.path"
 } }
 +
 +
 +
  
 # ----------------------------------------- # -----------------------------------------
Строка 400: Строка 393:
 Write-Host "" Write-Host ""
 Write-Host "=========================================" -ForegroundColor Cyan Write-Host "=========================================" -ForegroundColor Cyan
-Write-Host " Repair Complete"                        -ForegroundColor Cyan+Write-Host " Check Complete"                         -ForegroundColor Cyan
 Write-Host "=========================================" -ForegroundColor Cyan Write-Host "=========================================" -ForegroundColor Cyan
 Write-Host "" Write-Host ""
Строка 407: Строка 400:
 </code> </code>
  
-<code powershell >+==== Исправление ==== 
 + 
 +<code powershell python_env_repair.ps1>
 # ========================================= # =========================================
 # File: python_env_repair.ps1 # File: python_env_repair.ps1
windows/portable_tools/python/start.1778749427.txt.gz · Последнее изменение:

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