Posts Tagged ‘ipdb’

Debugging in Python

April 24, 2011 4 comments

If you are doing debugging with “print“, it’s time to try one of the following methods.

Debugging with pdb/ipdb

If you want a clear and gentle introduction to the usage of the Python debugger “pdb“, read Steve Ferg’s excellent tutorial Debugging in Python.

Here I make a short summary for reference purposes:

  • import pdb” or “import ipdb as pdb“, then “pdb.set_trace()
  • n (next)
  • ENTER (repeat previous)
  • q (quit)
  • p <variable> (print value)
  • c (continue)
  • l (list where you are)
  • s (step into subroutine)
  • r (continue till the end of the subroutine)
  • ! <python command>

ipdb is like pdb but it adds syntax highlightning and completion. You can install it with “sudo pip install ipdb“. If you used pdb with “import pdb“, just change this line to “import ipdb as pdb“. This way the line “pdb.set_trace()” can be left unchanged.

Debugging with Winpdb

Another interesting debugger is Winpdb, which is a platform independent GUI debugger for Python with support for multiple threads, namespace modification, embedded debugging, encrypted communication… It can be installed from the Ubuntu repos (sudo apt-get install winpdb). Tutorial here.

Short summary again:

  • restart (restart debugging session)
  • exit
  • n (next)
  • go (continue)
  • x <python command> (exec, changes state)
  • v <python command> (eval, print to console, no changes in state)
  • j <line> (jump)
  • s (step in subroutine)
  • r (return from subroutine)
  • bp <line> (breakpoint @ line)
  • bp <line>, <expression> (conditional breakpoint @ line)
  • bl (breakpoint list)
  • bc <id> | * (breakpoint clear)

Debugging with the Eric IDE

I would sum up how to debug with the Eric IDE too:

  • F5 (start debugging; untick “Don’t stop at first line” or set a breakpoint)
  • F10 (stop)
  • F7 (next, step in subroutines)
  • F8 (next, step over subroutines)
  • F9 (return, step out of subroutine)
  • F6 (continue, go, run)
  • Shift+F6 (continue till cursor)
  • conditional breakpoints are supported (set a breakpoint, right click on it, edit)

I find Eric’s debugger is much faster than Winpdb.

Update (20141227)
If you want to add a break point to your script and you want to “break out” to ipython, then just add this line:

import ipdb; ipdb.set_trace()
Categories: python Tags: , , , , ,