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.
import pdb msg = "this is a test" pdb.set_trace() print(msg)
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:
  1. b: set a breakpoint
  2. c: continue debugging until you hit a breakpoint
  3. s: step through the code
  4. n: to go to next line of code
  5. l: list source code for the current file
  6. u: navigate up a stack frame
  7. d: navigate down a stack frame
  8. p: to print the value of an expression in the current context
  9. q: quit
Running from command line of python interpreter.
python -m pdb scriptName.py
If you don't like spending time in debuggers, you can dump execution trace and analyse it later.
python -m trace -t scriptName.py install > debug.log