CARVIEW |
Select Language
HTTP/2 200
date: Sat, 11 Oct 2025 15:27:16 GMT
server: Fly/6f91d33b9d (2025-10-08)
content-type: text/html; charset=utf-8
content-encoding: gzip
via: 2 fly.io, 2 fly.io
fly-request-id: 01K79Y48R8QXM9KQW2FZHPSP1N-bom
The pdb interact command | Simon Willison’s TILs
The pdb interact command
Today Carlton told me about the interact command in the Python debugger.
Here's how to use it with pytest
(but it works anywhere else where you find yourself in a pdb
session).
Use pytest --pdb
to cause pytest
to open a debugger at the first failed assertion (I added assert False
to my test suite to demonstrate this).
Then type interact
to drop into a full Python interactive prompt that keeps all of the local and global variables from the debugger:
% pytest -k test_drop --pdb
======== test session starts ========
platform darwin -- Python 3.10.3, pytest-7.1.3, pluggy-1.0.0
...
> assert False
E assert False
tests/test_api_write.py:272: AssertionError
>>>> entering PDB >>>>
>>>> PDB post_mortem (IO-capturing turned off) >>>>
> /Users/simon/Dropbox/Development/datasette/tests/test_api_write.py(272)test_drop_table()
-> assert False
(Pdb) interact
>>> locals().keys()
dict_keys(['__name__', '__doc__', '__package__', '__loader__', '__spec__', '__file__', '__cached__', '__builtins__',
'@py_builtins', '@pytest_ar', 'Datasette', 'sqlite3', 'pytest', 'time', 'ds_write', 'write_token', 'test_write_row',
'test_write_rows', 'test_write_row_errors', 'test_delete_row', 'test_drop_table', 'scenario', 'token', 'should_work',
'path', 'response', '@py_assert0', '@py_format2'])
Crucially, once you are in the interactive prompt you can inspect local variables with names like s
and c
without accidentally triggering matching debugger commands.
Hit Ctrl+D
to exit back to the debugger:
>>> <Ctrl+D>
now exiting InteractiveConsole...
(pdb)
Related
- python Debugging a Click application using pdb - 2020-09-03
- python Tracing every executed Python statement - 2021-03-21
- pytest Snapshot testing with Syrupy - 2023-09-26
- pytest Treating warnings as errors in pytest - 2022-04-01
- docker Running gdb against a Python process in a running Docker container - 2021-03-21
- docker Run pytest against a specific Python version using Docker - 2022-09-05
- pytest Using pytest and Playwright to test a JavaScript web application - 2022-07-24
- datasette Using pytest-httpx to run intercepted requests through an in-memory Datasette instance - 2023-07-24
- python Using psutil to investigate "Too many open files" - 2022-10-13
- pytest pytest coverage with context - 2022-03-04
Created 2022-10-31T12:01:09-07:00 · Edit