python-debug-pattern
Skill này làm gì
Skill này cho AI agent một cách lặp lại được để chạy script Python và tìm ra lý do chúng thất bại. Nó dành cho bất kỳ ai cần thông tin lỗi rõ ràng từ một script.
Khi nào nên dùng
- ✓Khi một script Python thất bại và bạn chỉ thấy một thông báo khó hiểu.
- ✓Khi script ghi vào một file và bạn cần xác nhận nó được lưu ở đâu.
- ✓Khi bạn muốn AI so sánh một lần chạy thành công với một lần thất bại.
Liên kết skill
https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/python-debug-pattern/SKILL.mdSao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.
Cách dùng
Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.
Tin nhắn có sẵn để dán
Dành cho Claude Code, Codex hoặc Cursor: chạy lệnh trong thư mục dự án của bạn và skill sẽ được cài đặt.
Tải tệp SKILL.md về và thả vào thư mục skill của agent.
Ví dụ cách nhờ AI
“Chạy script process_data của tôi và cho tôi biết chính xác vì sao nó lỗi, kèm toàn bộ thông báo lỗi.”
5 skill mới mỗi tuần. Đọc chỉ 1 phút.
Cách hoạt động
Agent chạy script với stderr được thu lại và in mã thoát. Nó ghi lại thư mục làm việc và đường dẫn tuyệt đối của mọi file mà script ghi vào.
Nội dung kỹ thuật (SKILL.md gốc)
Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.
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 |
Câu hỏi thường gặp
Traceback là gì?
Traceback là danh sách các bước Python đã thực hiện trước khi xảy ra lỗi, và nó cho thấy vấn đề xảy ra ở đâu.
Tại sao ghi lại đường dẫn tuyệt đối của file đầu ra?
Nó cho thấy chính xác file được lưu ở đâu, điều này hữu ích khi bạn không tìm thấy nó.
Tôi có thể dùng mà không sửa script không?
Có, chỉ lệnh chạy cũng đã hiện lỗi, dù các dòng chẩn đoán sẽ hữu ích hơn.
Chi tiết
Mô tả gốc của tác giả
Debug Python script execution failures by capturing full output with exit codes and verifying working directory before file operations
repo: HKUDS/AgentPilot
Độ phổ biến
- Sao GitHub
- 7.8k
- Vị trí xếp hạng
- #12957
- Giấy phép
- MIT
- Cập nhật
- 1/11/2026
Bình luận
- …
Thêm skill từ kho mã này
agentpilot
7.8kGiúp agent của bạn cải thiện dự án từng bước bằng cách chuẩn bị tác vụ và chạy vòng lặp AgentPilot, gồm đề xuất, kiểm thử và chấm điểm thay đổi.
openspace
7.8kGiúp agent của bạn thiết lập OpenSpace, công cụ lưu và lấy lại skill của agent, rồi kết nối nó với ứng dụng AI của bạn qua MCP.
rsi-arena
7.8kGiúp agent của bạn thiết lập và chạy các bài kiểm thử benchmark AI chuẩn, như AIME và Terminal-Bench, để so sánh các phương pháp cải tiến khác nhau.
experiment-guide
7.8kGiúp agent của bạn lên kế hoạch các thí nghiệm nhỏ, đọc kết quả và ghi lại phát hiện khi tìm cách cải thiện một chương trình.
Skill tương tự
diffs
394kGiúp agent của bạn tạo một diff có thể chia sẻ về các chỉnh sửa, dưới dạng liên kết xem hoặc file ảnh, thay vì một bản tóm tắt viết tay.
coding-agent
394kGiúp agent của bạn giao một việc lập trình lớn cho Codex, Claude Code hoặc OpenCode chạy nền, rồi theo dõi tiến độ của nó.
gh-issues
394kGiúp agent của bạn tìm issue trên GitHub, chọn những issue cần xử lý và mở pull request với bản sửa, kể cả theo dõi nhận xét đánh giá.