# Python Debugging Covers CPython 3.9+, pytest, asyncio, Django, FastAPI. Setup commands, attach mechanisms, state-query patterns, gotchas, silent-failure signatures. --- ## Environment detection (Phase 0) ```bash # Which Python will actually run the code? which python; which python3 python --version # Is there a project env manager in play? ls poetry.lock uv.lock Pipfile.lock requirements*.txt .python-version 2>/dev/null # Installed debuggers / profilers in this env? python -c 'import pdb, sys; print("pdb", "built-in"); print("python", sys.executable)' pip list 2>/dev/null | grep -iE '^(ipdb|pudb|debugpy|py-spy|memray|rich)\s' # asyncio debug mode available? python -c 'import asyncio; print(asyncio.__version__)' ``` **Wrapper gotchas** (these change how flags propagate): - `poetry run python ...` — args after `python` are fine; args before `poetry run` go to poetry, not python - `uv run python ...` — similar; prefer `uv run -- python -X dev` if flags collide - `pipenv run` — same story - `./manage.py ` (Django) — shebang resolution; make sure it points to the right venv - `pytest` — loads `conftest.py` at collection; breakpoints inside collection need `pytest --pdb-trace` not `--pdb` --- ## The four ways to attach | Method | When to use | Command | |---|---|---| | **`breakpoint()` inline** (Python 3.7+) | You can edit the source and restart. Most reliable. | Add `breakpoint()` to source. Run normally. It invokes `pdb` by default. | | **`python -m pdb