Python Debugging
pdb
Keyboard Shortcuts
Escaping Variables Which Conflict With PDB Commands
Sometimes, you will run to run Python commands through the PDB console which will clash with PDB’s own command set. One example would be if you had a variable called c, and wanted to assign to it:
Instead of assigning the variable c
the value 23
, PDB will interpret the above c
as it’s continue command. To prevent this from happening, prefix the command with !
:
Entering A Running Python Process
pyrasite-shell is a great tool for attaching to a running Python process. You can install it using pip
with:
You can then attach a pyrasite shell to a running python process using the process ID (PID):
You should then be presented with an interactive Python shell, and you can do things like print the stack trace for each thread:
Note that annoyingly, coping and pasting this code into the interactive console will not usually work (due to whitespace getting mangled perhaps?).
pyrasite-shell
is a great tool for attaching to “hung” or “frozen” Python processes, which might be stuck in a loop or stuck in a blocked state waiting for something to happen.