Tried a couple of ways to debug python code, including using the %pdb magic within IPython or use %debug in the postmortem mode. Still couldn't figure out how to restart the code within the pdb invoked by the pdb magic without pdb raising a "restart" error.

So I think maybe the best way is to stick with the ipython debugger, ipdb which has autocomplete and syntax highlighting:

$ python -m ipdb myscript.py

and insert breakpoints with:

$ b mymodule.py:lineNum

and  debug until the next break point or restart by typing the shortcut for continue:

$ c

which behaves more similarly to gdb that I 'm more used to.

if you want to debug a certain function from another module, set a break point to step inside that function by:

$ from module.py import function
$ b function

and list all the arguments for this particular function:

$ args

We can also execute python code by:

$ !my_python_code_or_command

Stepping through the code is also the same as gdb

$ step  # or s

One of my favorite commands is actually

$ up

which moves the debugger state to one state up the stack trace. i.e. previous line executed

These are all the functionalities of pdb that I make use of so far.

Other useful tips can be found at http://docs.python.org/2/library/pdb.html

Trying to say goodbye to debugging with print statements.


Comments

comments powered by Disqus