Debugging with Pdb

How to use Pdb in your program
To use Pdb (Python DeBugger) we need to import pdb and set traces where you want the tracing to begin. import pdb names = ("Renee", "Dan", "Jonny", "Joel") pdb.set_trace # Trace will commence here. for each in names: print("Hello %s" % each) Now when running the code in a terminal it will look something like this: D:\pythonStuff\learning\debugging λ python epdb2.py                                                                                                                   > d:\pythonstuff\learning\debugging\epdb2.py(5) -> for each in names: (Pdb)

Commands
When you execute your python program in the command line and you get the above with the (Pdb) promt you can use some of the following commands.

Next (n)
Press the lower-case "n" and press enter. This will tell pbd to execute the next line of code. You can use this to step through your code line by line.

Repeating last command (enter)
By pressing "enter" at the prompt, pbd will execute the last command you used.

Quit (q)
This will exit you out and back to the regular command prompt.

Print (p)
This allows you to print the value of a variable in your code. For example with the code above, if I type: p names it will return: ('Renee', 'Dan', 'Jonny', 'Joel') You can also print multiple variables by typing: p variable1, variable2, variable3 Note that if you try and print a variable name before that declaration has been executed, you will get a NameError exception.

Continue (c)
If you wish to stop debugging and just let your code execute on it's own, press "c".

List (l)
"l" (lower-case L) shows you the area of code you are currently executing. By default it lists 11 lines of code. The line of code you are about to execute (the current line) is displayed with an arrow ("->") pointing to it.

Stepping into subroutines (s)
While stepping through a program, when you come up to a function/subroutine you may want to "step into" it. To enter the subroutine, step through the program till you get up to the line of code that calls said subroutine, and press "s". This will allow you to start debugging the code inside the subroutine.

Continue to end of current subroutine (r)
If you want to skip  ahead to the end of the subroutine, you can press "r" ("continue till return"). This will allow you to skip to the end of the subroutine and go back to stepping through the code.