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

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。它会读取这个技能并按说明操作。

可直接粘贴的消息

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

可以这样问 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>&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
常见问题

为什么要列出文件夹内容?

看到有哪些文件存在,往往能说明为什么找不到某个文件。

这只适用于某一种脚本吗?

不。这些步骤适用于任何读写文件的 Python 脚本。

我需要懂 Python 吗?

不需要。智能体会处理命令,并解释每个输出的含义。

详情

作者的原始描述

Debug Python scripts with proper error surfacing and working directory verification

repo: HKUDS/AgentPilot

热度

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

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能