Chuyển đến nội dung
Skillpedia
Lập trình chung7.8k · #12956

python-debug-execution

Skill này làm gì

Skill này giải thích một cách đơn giản để AI agent chạy và gỡ lỗi mã Python với đầu ra lỗi rõ ràng. Nó dành cho những người chạy script Python thông qua AI.

Khi nào nên dùng

  • ✓Khi một script Python dừng lại và bạn không biết vì sao.
  • ✓Khi các thao tác file trong script thất bại và thư mục có thể sai.
  • ✓Khi bạn muốn AI liệt kê nội dung thư mục để kiểm tra các file có đúng chỗ không.

Liên kết skill

https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/python-debug-execution/SKILL.md

Sao 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

Hãy dùng skill này để giúp tôi: https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/python-debug-execution/SKILL.md Đọc tệp SKILL.md tại liên kết đó và làm theo hướng dẫn cho việc của tôi.
Bạn muốn AI tự tìm những skill như thế này?Cài MCP Skillpedia miễn phí: chỉ một dòng, và AI sẽ tìm trong 15.000+ skill trước mỗi việc.Cài đặt trong 1 phút →

Ví dụ cách nhờ AI

“Script của tôi liên tục lỗi ở đường dẫn file. Hãy giúp tôi gỡ lỗi và cho tôi xem nó đang chạy ở thư mục nào.”

5 skill mới mỗi tuần. Đọc chỉ 1 phút.

Cách hoạt động

Agent chạy script với đầu ra lỗi được thu lại và in mã thoát. Nó thêm các kiểm tra in thư mục làm việc và các file trong đó trước khi script đọc hoặc ghi bất cứ thứ gì.

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 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>&1 captures both stdout and stderr, ensuring tracebacks are visible
  • echo 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
Câu hỏi thường gặp

Tại sao phải liệt kê nội dung thư mục?

Việc thấy file nào có mặt thường cho thấy vì sao không tìm thấy một file.

Điều này chỉ áp dụng cho một loại script thôi sao?

Không, các bước này áp dụng cho mọi script Python có đọc hoặc ghi file.

Tôi có cần biết Python không?

Không cần, agent sẽ xử lý các lệnh và giải thích ý nghĩa của từng kết quả.

Chi tiết

Mô tả gốc của tác giả

Debug Python scripts with proper error surfacing and working directory verification

repo: HKUDS/AgentPilot

Độ phổ biến

Sao GitHub
7.8k
Vị trí xếp hạng
#12956
Giấy phép
MIT
Cập nhật
1/11/2026

Skill này có hữu ích không?

0

Đăng nhập để bình chọn.

Bình luận

  • …

Thêm skill từ kho mã này

Skill tương tự