← 返回列表
全部 Skill›工具 Tools›python-debugpy

python-debugpy

作者 @NousResearch · 语言 en
Debug Python: pdb REPL + debugpy remote (DAP).
工具 Tools调试工具通用执行命令访问网络待翻译
最近源活动
—
检测 SKILL.md 语言
en
星标 ★
265,921
分支 Fork
0
安装选项
提示
指令
使用 AI 助手安装
将下面的提示复制到你正在使用的 AI 助手中。
下载完整 Skill 目录(含全部配套文件)。 下载压缩包 ↓
SKILL.md 原文预览

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 search_files with pattern="breakpoint\(\)", file_glob="*.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

and activate that checkout — PowerShell: . .\activate.ps1. The declared dev

extra includes debugpy, which PM activation does not sync (all excludes it).

Through terminal, build a fresh, caller-owned debug/test environment with the

prepared checkout's Python:

source ./activate
python -m pm.build_env --source . --out .venv --group dev --group test
.venv/bin/python -c "import debugpy; print(debugpy.__file__)"

The output must not already exist. Stop its processes and intentionally remove

only that disposable environment before rebuilding. Keep the same isolated

HERMES_HOME for the debug target. .venv/bin/python is this explicitly built

debug environment, not a guessed application venv, and the patterns below run

through it. Do not add debugpy to a running production environment; reproduce

there only with an already-prepared debug target or arrange a restart in the

development environment.

Pattern A: Source-edit — process waits for debugger at launch

Add near the top of the entry point (or inside the function you want to debug):

import debugpy
debugp
评论
NNousResearch
NousResearch
NousResearch/hermes-agent
查看 GitHub 仓库 查看作者的其他 Skill
来源信息
作者@NousResearch
所属大类工具 Tools / 调试工具
国内可用性通用
最近源活动—
风险提示执行命令、访问网络
提示:风险标签为程序静态扫描结果,仅作提示,不代表安全认证。安装前请阅读 SKILL.md 及其引用的脚本、权限,自行判断是否可信。