python-debug-pattern
它能做什么
这项技能为 AI 智能体提供了一种可重复的方法,用于运行 Python 脚本并找出失败原因。它适合需要脚本给出清晰错误细节的人。
适用场景
- ✓当 Python 脚本失败,而你只看到一条含糊的信息的时候。
- ✓当脚本写入文件,而你需要确认文件被写到哪里的时候。
- ✓当你希望 AI 比较一次成功运行与一次失败运行的时候。
技能链接
https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/python-debug-pattern/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请运行我的 process_data 脚本,并准确告诉我它为什么失败,包括完整的错误信息。”
每周 5 个新技能,1 分钟读完。
工作原理
智能体会在捕获 stderr 的情况下运行脚本,并打印退出代码。它会记录工作目录,以及脚本所写入任何文件的绝对路径。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Python Debug Pattern
This skill provides a reusable pattern for debugging Python script execution failures. It ensures you capture actual tracebacks instead of opaque errors and verify the working directory before file operations.
Core Technique
1. Execute Python Scripts with Full Error Capture
When running Python scripts, always use this command pattern to surface actual tracebacks:
python3 script.py 2>&1 ; echo Exit code: $?
Why this works:
2>&1redirects stderr to stdout, capturing both regular output and errors; echo Exit code: $?displays the actual exit code after execution- This reveals the full Python traceback instead of opaque failure messages
Examples:
# Good: Full error capture
python3 process_data.py 2>&1 ; echo Exit code: $?
# Bad: Opaque error (no stderr capture, no exit code)
python3 process_data.py
2. Verify Working Directory Before File Operations
Before any file read/write operations in Python, verify the current working directory:
import os
# At script start, log the working directory
print(f"Working directory: {os.getcwd()}")
# For file operations, use absolute paths or log the resolved path
file_path = "output/result.csv"
abs_path = os.path.abspath(file_path)
print(f"Writing to: {abs_path}")
Why this works:
- Many failures occur because scripts run from unexpected directories
- Logging the working directory immediately surfaces path-related issues
- Absolute paths prevent ambiguity in file operations
Full example script structure:
#!/usr/bin/env python3
import os
import sys
def main():
# Debug: verify working directory
print(f"Working directory: {os.getcwd()}", file=sys.stderr)
# Debug: list directory contents if dealing with files
print(f"Directory contents: {os.listdir('.')}", file=sys.stderr)
# Your actual logic here
# ...
if __name__ == "__main__":
main()
When to Apply This Pattern
- Python script fails with an opaque error message
- File operations (read/write) fail unexpectedly
- Script works locally but fails in automated execution
- Debugging CI/CD pipeline failures
- Investigating "file not found" or permission errors
Quick Checklist
- Run script with
2>&1 ; echo Exit code: $?pattern - Add
os.getcwd()logging at script start - Use
os.path.abspath()for file paths - Log directory contents with
os.listdir('.')if relevant - Check for missing dependencies (capture full traceback)
Common Failure Modes This Pattern Surfaces
| Symptom | Without Pattern | With Pattern |
|---|---|---|
| File not found | "Error: failed" | Full traceback showing exact path attempted |
| Permission denied | Script exits silently | stderr shows permission error |
| Missing module | Opaque exit | ImportError with module name |
| Wrong directory | Confusing path errors | cwd logged, reveals directory mismatch |
常见问题
什么是堆栈跟踪?
堆栈跟踪是 Python 在出错前执行的步骤列表,它显示问题发生在哪里。
为什么要记录输出文件的绝对路径?
它能准确显示文件的保存位置,在你找不到文件时很有帮助。
不修改脚本也能使用吗?
可以。仅运行命令就能显示错误,不过诊断代码行的帮助更大。
详情
作者的原始描述
Debug Python script execution failures by capturing full output with exit codes and verifying working directory before file operations
repo: HKUDS/AgentPilot
热度
- GitHub 星标
- 7.8k
- 排名
- #12957
- 许可证
- MIT
- 更新时间
- 2026/11/1
评论
- …
同一仓库的更多技能
agentpilot
7.8k帮你的智能体准备任务并运行 AgentPilot 循环,逐步改进项目:循环会提出修改、测试修改并为其打分。
openspace
7.8k帮你的智能体设置 OpenSpace,这是一个用于存储和获取智能体技能的工具,并通过 MCP 将其连接到你的 AI 应用。
rsi-arena
7.8k帮你的智能体设置并运行标准 AI 基准测试,例如 AIME 和 Terminal-Bench,用来比较不同的改进方法。
experiment-guide
7.8k帮你的智能体规划小型实验、阅读实验结果并记录发现,同时研究如何改进一个程序。