python-debug-pattern
できること
このスキルは、Pythonのスクリプトを実行し、なぜ失敗するのかを突き止めるための、繰り返し可能な方法をAIエージェントに与えます。スクリプトから明確なエラーの詳細が必要な人向けです。
使う場面
- ✓Pythonのスクリプトが失敗し、不透明なメッセージしか見えないとき。
- ✓スクリプトがファイルに書き込み、それがどこに行ったかを確かめる必要があるとき。
- ✓うまくいった実行と失敗した実行を、AIに比べさせたいとき。
スキルのリンク
https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/python-debug-pattern/SKILL.mdコピーしてAI(Claude、ChatGPT、Cursor)に貼り付け、指示に従うよう頼んでください。
使い方
いちばん簡単な方法:メッセージをコピーしてAIに貼り付けるだけ。AIがスキルを読んで指示に従います。
貼り付け用メッセージ
Claude Code、Codex、Cursorの場合:プロジェクトのフォルダで実行するとスキルがインストールされます。
SKILL.mdをダウンロードして、エージェントのスキル用フォルダに入れてください。
AIへの頼み方の例
“process_dataスクリプトを実行して、なぜ失敗するのかを、完全なエラーも含めて正確に教えてください。”
毎週5つの新しいスキル。1分で読めます。
仕組み
エージェントは、標準エラーを取り込んでスクリプトを実行し、終了コードを表示します。作業ディレクトリと、スクリプトが書き込むファイルの絶対パスを記録します。
技術的な内容(元の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エージェントが、AIMEやTerminal-Benchなどの標準的なAIベンチマークテストをセットアップして実行し、異なる改善方法を比較できるよう支援します。
experiment-guide
7.8kエージェントが、プログラムを改善する方法を調べる間に、小さな実験を計画し、その結果を読み、発見を記録できるよう支援します。
似ているスキル
diffs
394k編集内容の実際の共有可能な差分を、手書きの要約ではなく、ビューアーのリンクや画像ファイルとして作成するようエージェントを支援します。
coding-agent
394kCodex、Claude Code、OpenCodeに大きめのコーディング作業をバックグラウンドのワーカーとして渡し、その進捗を確認するようエージェントを支援します。
gh-issues
394kGitHubのイシューを探し、取り組むものを選び、修正を含むプルリクエストを開くようエージェントを支援します。レビューコメントへの対応も含みます。