跳到正文
Skillpedia
通用编程7.8k · #12955

python-debug-execution-911f17

它能做什么

这项技能向 AI 智能体展示如何运行 Python 脚本,使完整的错误信息显示出来,而不是含糊的提示。它适合 Python 脚本失败、并希望找到真正原因的人。

适用场景

  • ✓当 Python 脚本失败,而错误信息没有提供有用细节的时候。
  • ✓当脚本找不到文件,而你怀疑使用了错误文件夹的时候。
  • ✓当你希望 AI 逐步调试脚本,并显示退出代码的时候。

技能链接

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

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/python-debug-execution-911f17/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“我的 Python 脚本失败了,但没有明确的错误信息。请运行它,让我看到完整的错误信息。”

每周 5 个新技能,1 分钟读完。

工作原理

智能体会运行脚本,把错误输出与正常输出发送到同一位置,并打印退出代码。它还会打印当前文件夹,便于发现路径问题。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Python Debug Execution

This skill provides a pattern for debugging Python script execution failures. It ensures you capture actual tracebacks instead of opaque errors and verifies the working directory before file operations.

Core Pattern

1. Execute with Full Traceback Capture

Always run Python scripts with stderr redirected and exit code reported:

python3 script.py 2>&1 ; echo Exit code: $?

This pattern:

  • 2>&1 - Redirects stderr to stdout so all output (including tracebacks) is captured together
  • echo Exit code: $? - Reports the exit code to distinguish between successful runs and failures

Why this matters: Opaque errors without tracebacks make it impossible to identify the root cause. The exit code tells you if the script succeeded (0) or failed (non-zero).

2. Verify Working Directory Before File Operations

Before any file read/write operations in your Python script, add:

import os
print(f"Current working directory: {os.getcwd()}")

Or for debugging, add at the start of your script:

import os
import sys

# Debug: show execution context
print(f"Script path: {__file__}")
print(f"Working directory: {os.getcwd()}")
print(f"Python version: {sys.version}")

Why this matters: File operations often fail due to incorrect assumptions about the current working directory. Verifying os.getcwd() helps diagnose path-related errors.

Usage Examples

Example 1: Running a Script with Debug Output

# Instead of:
python3 analyze.py

# Use:
python3 analyze.py 2>&1 ; echo Exit code: $?

Example 2: Script with Directory Verification

#!/usr/bin/env python3
import os
import pandas as pd

# Verify execution context
print(f"Working directory: {os.getcwd()}")

# Now safe to do file operations
data_path = "data/input.csv"
print(f"Attempting to read: {data_path}")

# Check if file exists before reading
if os.path.exists(data_path):
    df = pd.read_csv(data_path)
    print(f"Successfully loaded {len(df)} rows")
else:
    print(f"ERROR: File not found at {os.path.abspath(data_path)}")
    print(f"Directory contents: {os.listdir('.')}")

Example 3: Debug-First Script Template

#!/usr/bin/env python3
"""Template for debuggable Python scripts."""

import os
import sys
import traceback

def main():
    # Debug: execution context
    print("=" * 50)
    print(f"Script: {__file__}")
    print(f"Working directory: {os.getcwd()}")
    print(f"Python: {sys.version}")
    print("=" * 50)
    
    try:
        # Your main logic here
        pass
    except Exception as e:
        print(f"ERROR: {type(e).__name__}: {e}")
        traceback.print_exc()
        sys.exit(1)

if __name__ == "__main__":
    main()

Troubleshooting Checklist

When a Python script fails:

  1. Check the exit code - Non-zero means failure
  2. Read the full traceback - Look for the actual error message and line number
  3. Verify working directory - Use os.getcwd() to confirm file paths are correct
  4. Check file existence - Use os.path.exists() before operations
  5. Use absolute paths - Consider os.path.abspath() for clarity

When to Apply This Pattern

  • Running any Python script from the command line
  • Debugging script execution failures in automated agents
  • Writing scripts that perform file I/O operations
  • Creating reproducible execution environments
常见问题

退出代码说明什么?

代码为 0 表示脚本已完成,其他数字则表示失败。

为什么要打印工作文件夹?

许多文件错误是因为脚本从与预期不同的文件夹运行而产生的。

这会改动我的脚本吗?

这项技能会添加几行用于调试的诊断代码,但不会改变脚本的逻辑。

详情

作者的原始描述

Debug Python script execution failures by capturing full tracebacks and verifying working directory

repo: HKUDS/AgentPilot

热度

GitHub 星标
7.8k
排名
#12955
许可证
MIT
更新时间
2026/11/1

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能