Debugging in Python
Python has a debugger , which is available as a module called pdb . It supports setting conditional breakpoints , stepping through the source code one line at a time, stack inspection, and more.
Insert pdb.set_trace() anywhere and it will function as a breakpoint . When you execute the script by python test.py, you will in the debug mode.
Some useful debugging commands:
- b: set a breakpoint
- c: continue debugging until you hit a breakpoint
- s: step through the code
- n: to go to next line of code
- l: list source code for the current file
- u: navigate up a stack frame
- d: navigate down a stack frame
- p: to print the value of an expression in the current context
- q: quit
Running from command line of python interpreter.
If you don't like spending time in debuggers, you can dump execution trace and analyse it later.