跳到正文
Skillpedia
通用编程260k · #574

python-debugpy

它能做什么

这项技能通过暂停代码并检查变量值,帮助你的 AI 找出 Python 程序中的错误。适合打印语句无法揭示问题的开发者。

适用场景

  • ✓Python 测试失败,而回溯信息没有说明某个值为什么不对的时候。
  • ✓你需要逐步执行某个函数,并观察运行时数据变化的时候。
  • ✓长时间运行的 Python 进程行为异常,而你又无法重启它的时候。

技能链接

https://github.com/NousResearch/hermes-agent/blob/main/optional-skills/software-development/python-debugpy/SKILL.md

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

使用方法

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

可直接粘贴的消息

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

可以这样问 AI

“我的 Python 函数返回了错误的列表,打印语句也没有帮助。你能和我一起逐步调试吗?”

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

工作原理

这项技能使用 Python 内置的 pdb 调试器,你可以在代码中设置断点,或在 pdb 下运行脚本来启动它。对于正在运行的进程,它使用 debugpy 工具进行远程附加。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Python Debugger (pdb + debugpy)

Overview

Three tools, picked by situation:

Tool When
breakpoint() + pdb Local, interactive, simplest. Add breakpoint() in the source, run normally, get a REPL at that line.
python -m pdb Launch an existing script under pdb with no source edits. Useful for quick poking.
debugpy Remote / headless / "attach to already-running process." Talks DAP, scriptable from terminal, works for long-lived processes (gateway, daemon, PTY children).

Start with breakpoint(). It's the cheapest thing that works.

When to Use

  • A test fails and the traceback doesn't reveal why a value is wrong
  • You need to step through a function and watch a collection mutate
  • A long-running process (hermes gateway, tui_gateway) misbehaves and you can't restart it
  • Post-mortem: an exception fired in prod-ish code and you want to inspect locals at the crash site
  • A subprocess / child (Python _SlashWorker, PTY bridge worker) is the actual bug site

Don't use for: things print() / logging.debug solve in under a minute, or things pytest -vv --tb=long --showlocals already reveals.

pdb Quick Reference

Inside any pdb prompt ((Pdb)):

Command Action
h / h cmd help
n next line (step over)
s step into
r return from current function
c continue
unt N continue until line N
j N jump to line N (same function only)
l / ll list source around current line / full function
w where (stack trace)
u / d move up / down in the stack
a print args of the current function
p expr / pp expr print / pretty-print expression
display expr auto-print expr on every stop
b file:line set breakpoint
b func break on function entry
b file:line, cond conditional breakpoint
cl N clear breakpoint N
tbreak file:line one-shot breakpoint
!stmt execute arbitrary Python (assignments included)
interact drop into full Python REPL in current scope (Ctrl+D to exit)
q quit

The interact command is the most powerful — you can import anything, inspect complex objects, even call methods that mutate state. Locals are read-only by default; use !x = 42 from the (Pdb) prompt to mutate.

Recipe 1: Local breakpoint

Easiest. Edit the file:

def compute(x, y):
    result = some_helper(x)
    breakpoint()           # <-- drops into pdb here
    return result + y

Run the code normally. You land at the breakpoint() line with full access to locals.

Don't forget to remove breakpoint() before committing. Use git diff or a pre-commit grep:

rg -n 'breakpoint\(\)' --type py

Recipe 2: Launch a script under pdb (no source edits)

python -m pdb path/to/script.py arg1 arg2
# Lands at first line of script
(Pdb) b path/to/script.py:42
(Pdb) c

Recipe 3: Debug a pytest test

Use terminal and the canonical runner for noninteractive diagnostics:

# Show locals in tracebacks without pdb:
scripts/run_tests.sh tests/path/to/test_file.py --showlocals --tb=long

scripts/run_tests.sh captures each file in a separate subprocess, so --pdb or --trace cannot provide an interactive prompt there. For an interactive debugger only, use the independent development/test interpreter prepared in Recipe 5 (never a production generation):

.venv/bin/python -m pytest tests/foo_test.py::test_bar --pdb

This bypasses the hermetic-env guarantees — fine for debugging, but re-run under the wrapper to confirm before pushing.

Recipe 4: Post-mortem on any exception

import pdb, sys
try:
    run_the_thing()
except Exception:
    pdb.post_mortem(sys.exc_info()[2])

Or wrap a whole script:

python -m pdb -c continue script.py
# When it crashes, pdb catches it and you're in the frame of the exception

Or set a global hook in a repl/jupyter:

import sys
def excepthook(etype, value, tb):
    import pdb; pdb.post_mortem(tb)
sys.excepthook = excepthook

Recipe 5: Remote debug with debugpy (attach to running process)

For long-lived processes: Hermes gateway, tui_gateway, a daemon, a process that's already misbehaving and can't be restarted clean.

Setup

For Hermes, use a separate development checkout and data home, not a live production generation. Follow the [PM developer workflow](https://hermes-agent.nousres

常见问题

最简单的入门方式是什么?

技能说明在代码中添加一行 breakpoint(),然后正常运行,就会在该行打开调试器提示符。

debugpy 用来做什么?

技能使用 debugpy 进行远程或无界面调试,以及调试已经在运行的进程。

什么时候不应使用它?

技能说明,如果打印语句或 pytest 输出能在一分钟内揭示问题,就不要使用它。

详情

作者的原始描述

Debug Python: pdb REPL + debugpy remote (DAP).

repo: NousResearch/hermes-agent

热度

GitHub 星标
260k
排名
#574
许可证
MIT
更新时间
2026/11/2

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能