Files
chengnan/docs/windows/setup-python-venv-windows.ps1
2026-05-29 22:29:38 +08:00

30 lines
1.1 KiB
PowerShell
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
param(
[string]$PythonBin = "$PSScriptRoot\..\tools\python-bin",
[string]$VenvDir = "$PSScriptRoot\..\tools\python-venv"
)
$ErrorActionPreference = "Stop"
$PythonBin = [System.IO.Path]::GetFullPath($PythonBin)
$VenvDir = [System.IO.Path]::GetFullPath($VenvDir)
$PythonExe = Join-Path $PythonBin "python.exe"
if (!(Test-Path $PythonExe)) {
throw "未找到项目内 Python 二进制:$PythonExe"
}
# Windows embeddable Python 不一定带 venv 模块。优先尝试 venv失败则使用同目录作为隔离 Python 环境安装 pip。
try {
& $PythonExe -m venv $VenvDir
$VenvPython = Join-Path $VenvDir "Scripts\python.exe"
& $VenvPython -m pip install --upgrade pip setuptools wheel
& $VenvPython -m pip install aqtinstall
Write-Host "已创建虚拟环境:$VenvDir"
} catch {
Write-Warning "venv 创建失败,改用项目内 embeddable Python 自身作为隔离环境。"
& $PythonExe (Join-Path $PythonBin "get-pip.py")
& $PythonExe -m pip install --upgrade pip setuptools wheel
& $PythonExe -m pip install aqtinstall
Write-Host "已在项目内 Python 二进制目录安装 pip/aqtinstall$PythonBin"
}