笔记 · Vault Pattern

Pattern: Windows Python subprocess bash

**发现日期**: 2026-07-28 **来源**: smoke-test.py 中 startup-check.sh 长期误判FAIL **严重度**: 🔴 高 1. `str(Path)` 在 Windows 上输出反斜杠 → bash 吃掉反斜杠

2026/07/30
日期
技术
分类
4
标签数

Pattern: Windows Python subprocess 调用 bash 路径翻译失败

发现日期: 2026-07-28 来源: smoke-test.py 中 startup-check.sh 长期误判FAIL 严重度: 🔴 高

触发条件

# Windows Python 中
TOOLS_DIR = Path("C:/Users/xinzh/.openclaw/tools/core")
subprocess.run(["bash", str(TOOLS_DIR / "startup-check.sh")])
# → /bin/bash: C:Usersxinzh.openclawtoolscorestartup-check.sh: No such file or directory
# → RC=127

根因三重

  1. str(Path) 在 Windows 上输出反斜杠 → bash 吃掉反斜杠
  2. Path.as_posix() 输出 C:/Users/... → Git Bash 不认识 C: 前缀(需要 /c/
  3. /c/ 前缀路径 → Windows Python os.path.exists() 返回 False

修复

# ✅ 使用 shell=True + 原生 Windows 路径
subprocess.run(f'bash "{TOOLS_DIR / "script.sh"}"', shell=True, ...)

预防规则

  • Windows + Python + bash 的三重环境组合:优先 shell=True
  • 所有Python→bash的调用路径测试必须在Windows上实际跑
  • CI中增加Windows专项测试
📌 源:D:\个人文件\AI\Patterns\pattern-windows-python-subprocess-bash-path.md
🔒 已脱敏:内部路径 / 任务 ID / 邮箱 / 电话 / 微信号
延伸阅读 · Related

看完这篇,下一步该看什么

同标签笔记 · 3
pattern-2026-07-27-FFmpeg版本兼容
场景: FFmpeg跨版本/跨平台(Linux vs Windows)滤镜语法差异大。Linux上有效的滤镜语法在Windows FFmpeg build上可能不识别(如`-loop 1`+`-vf`组合、`if(eq(...))`条件表达式)。
同标签:技术
pattern-2026-07-27-GPU-CUDA验证
场景: Python项目中依赖GPU加速(PyTorch/CUDA),nvidia-smi显示GPU可用但实际torch安装的是CPU-only版本,导致运行时CUDA错误。环境检查不充分导致反复试错。
同标签:技术
Pattern: set -e VAR bash
**发现日期**: 2026-07-28 **来源**: startup-check.sh 长期存在但未被发现的bug **严重度**: 🔴 高(导致所有带警告的检查被误判为失败) bash的 `((expr))` 算术表达式:表达式结果为0时返回exit 1。配合 `set -e`,脚本立即终止。
同标签:技术
底层原理 · 2
实战 Agent · 2
行业案例 · 1