The Python debugger pdb
While debugging, you might need to stop a Django application in the middle of execution to examine its state. A simple way to achieve this is to raise an exception with a simple assert False
line in the required place.
What if you wanted to continue the execution step by step from that line? This is possible with the use of an interactive debugger such as Python's pdb
. Simply insert the following line wherever you want the execution to stop and switch to pdb
:
import pdb; pdb.set_trace()
Once you enter pdb
, you will see a command-line interface in your console window with a (Pdb)
prompt. At the same time, your browser window will not display anything, as the request has not finished processing.
The pdb
command-line interface is extremely powerful. It allows you to go through the code line by line, examine the variables by printing them, or execute arbitrary code that can even change the running state. The interface is quite similar to GDB, the GNU debugger.