Debugging Python with Pdb!

noelled 2,233 views 13 slides Jul 15, 2015
Slide 1
Slide 1 of 13
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13

About This Presentation

A brief introduction python's handy built-in debugger, Pdb. Presented as a part of the Hackbright Academy summer fellowship.


Slide Content

Debugging Python with Pdb Noelle Daley, 7.13.2015

Debugging numbers = [1, 2, 3, 4, 10, -4, -7, 0] def all_even(l): even_numbers = [] for number in l: if number / 2 == 0: even_numbers.extend(number) return even_numbers all_even(numbers)

One option: all the print statements! for number in l: print "Number is: ", number if number / 2 == 0: print number, " is an even number!" even_numbers.extend(number) print "List now includes:" print even_numbers

Enter Pdb! Pdb is the built-in Python debugger. It helps you debug programs interactively, allowing you to step into functions, test variables, and much more. *not really

Using Pdb Import pdb, then insert where you’d like to start debugging: numbers = [1, 2, 3, 4, 10, -4, -7, 0] import pdb; pdb.set_trace() def all_even(l): even_numbers = []...

Using Pdb

Pdb Commands l (list) -- shows where you are in your script n (next) -- steps to next line of execution (but not into functions!) s (step) -- steps into next line of execution, including functions r (return) -- steps to the end of currently executing function c (continue) -- executes code q (quit) -- exit the debugger ? (help) -- shows all commands

Pbd Commands: l List where you are in the script:

Pbd Commands: n Step to next line of execution:

Pbd Commands: s Step into next line of execution:

Evaluating Variables!!!!!!!!!! 1 / 2 == 0?!

Recap Pdb is awesome! Use it when print statements are cluttering your script. Import: import pdb; pdb.set_trace() Navigate with l , n , and s .

Resources Debugging in Python - Python Conquers the Universe Python Docs - the Python Debugger Tutorial: Debugging Python apps with Pdb