71 lines
2.9 KiB
PowerShell
71 lines
2.9 KiB
PowerShell
# =============================================================================
|
|
# setup_venv.ps1 —— 在 Windows 上创建标准 Python venv,不污染系统 Python。
|
|
#
|
|
# 重要前提:
|
|
# - Windows 上 rclpy / sensor_msgs / cv_bridge 没有官方 pip wheels,
|
|
# 所以 venv 里 *只能装纯 Python 工具*(ruff / black / mypy / pytest / numpy)。
|
|
# - 真正的 ROS2 节点必须在 Docker Desktop 容器内 / WSL Ubuntu 内运行。
|
|
# - 本机用 VSCode / PyCharm 等 IDE 时,把 interpreter 指向 .venv\Scripts\python.exe
|
|
# 即可享受自动补全 / 类型检查,即使 rclpy 解析不了也没关系
|
|
# (pyrightconfig.json 已配置 ignore_missing_imports)。
|
|
#
|
|
# 使用:
|
|
# PS> .\tools\setup_venv.ps1
|
|
# PS> .\.venv\Scripts\Activate.ps1
|
|
# =============================================================================
|
|
|
|
$ErrorActionPreference = 'Stop'
|
|
|
|
# 切到脚本所在目录的上一级(项目根)
|
|
Set-Location -LiteralPath (Join-Path $PSScriptRoot '..')
|
|
|
|
# 优先 python,找不到再 python3
|
|
$PYTHON = $null
|
|
foreach ($cand in @('python', 'python3', 'py')) {
|
|
$cmd = Get-Command $cand -ErrorAction SilentlyContinue
|
|
if ($cmd) { $PYTHON = $cand; break }
|
|
}
|
|
if (-not $PYTHON) {
|
|
throw 'Python not found in PATH. Install Python 3.10+ from python.org first.'
|
|
}
|
|
|
|
Write-Host "==> Using: $PYTHON" -ForegroundColor Cyan
|
|
& $PYTHON --version
|
|
|
|
$VENV_DIR = '.venv'
|
|
|
|
# 1) 创建 venv(若已存在则提示并复用)
|
|
if (-not (Test-Path -LiteralPath $VENV_DIR)) {
|
|
Write-Host "==> Creating venv at $VENV_DIR" -ForegroundColor Cyan
|
|
& $PYTHON -m venv $VENV_DIR
|
|
} else {
|
|
Write-Host "==> Reusing existing venv at $VENV_DIR" -ForegroundColor Yellow
|
|
}
|
|
|
|
$venvPython = Join-Path $VENV_DIR 'Scripts\python.exe'
|
|
if (-not (Test-Path -LiteralPath $venvPython)) {
|
|
throw "venv python.exe not found at $venvPython"
|
|
}
|
|
|
|
# 2) 升级 pip + 装开发依赖
|
|
Write-Host "==> Upgrading pip + installing requirements-dev.txt" -ForegroundColor Cyan
|
|
& $venvPython -m pip install --upgrade pip wheel setuptools
|
|
& $venvPython -m pip install -r requirements-dev.txt
|
|
|
|
# 3) pip install -e 把本项目 ROS2 Python 包装到 venv(纯 Python 部分,不动 ROS 客户端)
|
|
Write-Host "==> Installing ROS2 Python packages (editable)" -ForegroundColor Cyan
|
|
foreach ($pkg in @('py_pubsub','py_srv','py_action_demo','py_vision_demo')) {
|
|
$pkgDir = Join-Path 'src' $pkg
|
|
if (Test-Path -LiteralPath $pkgDir) {
|
|
& $venvPython -m pip install -e $pkgDir --no-deps
|
|
Write-Host " OK $pkg" -ForegroundColor Green
|
|
}
|
|
}
|
|
|
|
Write-Host ''
|
|
Write-Host 'venv ready. Activate with:' -ForegroundColor Green
|
|
Write-Host ' .\.venv\Scripts\Activate.ps1' -ForegroundColor Green
|
|
Write-Host ''
|
|
Write-Host 'Next steps (Windows):' -ForegroundColor Yellow
|
|
Write-Host ' pytest src/py_pubsub/test -m "not ros" # 本机纯逻辑测试'
|
|
Write-Host ' docker exec ros2_dev bash -lc "cd /root/ros2_ws && colcon test" # 容器内 ROS2 测试' |