python-debug-execution
它能做什么
这项技能说明了一种简单的方法,让 AI 智能体运行并调试 Python 代码,同时输出清晰的错误信息。它适合通过 AI 运行 Python 脚本的人。
适用场景
- ✓当 Python 脚本停止运行,而你无法判断原因的时候。
- ✓当脚本中的文件操作失败,而文件夹可能不对的时候。
- ✓当你希望 AI 列出文件夹内容,以确认文件位于正确位置的时候。
技能链接
https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/python-debug-execution/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“我的脚本一直在文件路径上失败。请帮我调试,并告诉我它正在哪个文件夹中运行。”
每周 5 个新技能,1 分钟读完。
工作原理
智能体会在捕获错误输出的情况下运行脚本,并打印退出代码。它还会添加检查,在脚本读取或写入任何内容之前,先打印工作目录及其中的文件。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Python Debug Execution Pattern
When executing Python scripts that may fail, use this pattern to surface clear error information and diagnose issues effectively.
Core Technique
1. Execute with Full Error Output
Always run Python scripts with stderr redirected to stdout and echo the exit code:
python3 script.py 2>&1 ; echo Exit code: $?
Why this works:
2>&1captures both stdout and stderr, ensuring tracebacks are visibleecho Exit code: $?reveals the actual exit status for debugging- Avoids opaque "command failed" errors that hide the real cause
2. Verify Working Directory in Script
Before any file operations in Python scripts, add working directory verification:
import os
# At the start of your script or before file operations
print(f"Current working directory: {os.getcwd()}")
# For debugging, also list directory contents
print(f"Directory contents: {os.listdir('.')}")
Why this works:
- File not found errors often stem from incorrect working directory assumptions
- Makes path-related failures immediately diagnosable
- Confirms the execution context matches expectations
Complete Debugging Workflow
Step 1: Add Diagnostic Code to Script
#!/usr/bin/env python3
import os
import sys
def main():
# Diagnostic: verify execution context
print(f"Working directory: {os.getcwd()}")
print(f"Python version: {sys.version}")
print(f"Directory listing: {os.listdir('.')}")
# Your actual logic here
# ...
if __name__ == "__main__":
main()
Step 2: Execute with Full Error Capture
python3 script.py 2>&1 ; echo Exit code: $?
Step 3: Analyze Output
Look for:
- Traceback messages (indicate code errors)
- Exit code (0 = success, non-zero = failure)
- Working directory confirmation
- Missing file/directory errors
Common Failure Patterns
| Symptom | Likely Cause | Debug Clue |
|---|---|---|
| FileNotFoundError | Wrong working directory | Check os.getcwd() output |
| ModuleNotFoundError | Missing dependencies | Traceback shows import path |
| PermissionError | File access issues | Traceback shows file path |
| Silent failure (exit 0, no output) | Logic bug, not crash | Add print statements |
When to Use This Pattern
- Running Python scripts in automated/agent contexts
- Debugging scripts that interact with files
- Troubleshooting CI/CD pipeline failures
- Any scenario where script output may be truncated or hidden
常见问题
为什么要列出文件夹内容?
看到有哪些文件存在,往往能说明为什么找不到某个文件。
这只适用于某一种脚本吗?
不。这些步骤适用于任何读写文件的 Python 脚本。
我需要懂 Python 吗?
不需要。智能体会处理命令,并解释每个输出的含义。
详情
作者的原始描述
Debug Python scripts with proper error surfacing and working directory verification
repo: HKUDS/AgentPilot
热度
- GitHub 星标
- 7.8k
- 排名
- #12956
- 许可证
- 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帮你的智能体规划小型实验、阅读实验结果并记录发现,同时研究如何改进一个程序。