


























@@ -0,0 +1,73 @@
1+---
2+name: python-debugpy
3+description: Debug Python with pdb, breakpoint(), post-mortem inspection, and debugpy remote attach.
4+metadata: { "openclaw": { "requires": { "bins": ["python3"] } } }
5+---
6+7+# Python Debugpy
8+9+Use when Python code needs interactive debugging: hidden locals, confusing state mutation, failing tests, subprocesses, long-running services, or remote/headless attach.
10+11+Pick the smallest debugger that reaches the bad frame.
12+13+## Choose
14+15+- `breakpoint()`: local code, source edits ok, fastest path.
16+- `python3 -m pdb`: no source edit, launch from the beginning.
17+- `python3 -m pdb -c continue`: stop at an unhandled exception.
18+- `debugpy`: remote/headless process, DAP client, already-running PID, or service startup race.
19+20+## Commands
21+22+```bash
23+python3 -m pdb path/to/script.py arg1
24+python3 -m pdb -c continue path/to/script.py
25+python3 -c "import debugpy" || python3 -m pip install debugpy
26+python3 -m debugpy --listen 127.0.0.1:5678 --wait-for-client path/to/script.py
27+python3 -m debugpy --listen 127.0.0.1:5678 --wait-for-client -m package.module
28+python3 -m debugpy --listen 127.0.0.1:5678 --pid <pid>
29+```
30+31+For source-edit attach:
32+33+```py
34+import debugpy
35+36+debugpy.listen(("127.0.0.1", 5678))
37+debugpy.wait_for_client()
38+debugpy.breakpoint()
39+```
40+41+For post-mortem:
42+43+```py
44+import pdb, sys
45+46+try:
47+ run()
48+except Exception:
49+ pdb.post_mortem(sys.exc_info()[2])
50+raise
51+```
52+53+## pdb
54+55+- Flow: `n`, `s`, `r`, `c`, `q`.
56+- Stack/source: `w`, `u`, `d`, `a`, `l`, `ll`.
57+- Values: `p expr`, `pp expr`, `display expr`.
58+- Breakpoints: `b file.py:42`, `b func`, `b file.py:42, condition`, `cl <num>`.
59+- Mutate/evaluate: `!statement`; full REPL: `interact`.
60+61+## Rules
62+63+- Reproduce with the smallest command/test first.
64+- Disable parallel test workers for pdb; interactive stdin usually breaks inside worker pools.
65+- Keep `debugpy` in the active env; do not add it as a project dependency unless the project already wants it.
66+- Bind debug servers to `127.0.0.1`; do not expose `0.0.0.0` unless isolated or tunnelled.
67+- Use unique ports for parallel sessions.
68+- Treat `debugpy --pid` as injection; avoid security-sensitive or production targets unless explicitly approved.
69+- If PID attach fails on Linux, check ptrace/container privileges before changing the target.
70+- Cleanup before commit: `rg -n 'breakpoint\\(|pdb\\.set_trace|debugpy\\.' --type py`.
71+- Rerun the normal project test/gate without the debugger.
72+- `PYTHONBREAKPOINT=0` disables `breakpoint()`.
73+- If a process is stuck after debugger detach, confirm it is not still paused at a breakpoint.
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。