Traceback (most recent call last):
File "/Users/dorota/teaching/pycon_debugging/part1/division.py", line 1, in <module>
1 / 0
ZeroDivisionError: division by zero
Traceback (most recent call last):
File "/Users/dorota/teaching/pycon_debugging/part1/assert.py", line 1, in <module>
assert 1 + 2 < 3
AssertionError
Python Traceback
Traceback (most recent call last):
File "/Users/dorota/teaching/pycon_debug/part2/standard_deviation.py", line 41, in <module>
main(filename)
File "/Users/dorota/teaching/pycon_debug/part2/standard_deviation.py", line 35, in main
std = standard_deviation_list(data)
File "/Users/dorota/teaching/pycon_debug/part2/standard_deviation.py", line 24, in standard_deviation_list
mean = mean_value_list(data_list)
File "/Users/dorota/teaching/pycon_debug/part2/standard_deviation.py", line 6, in mean_value_list
return sum(data_list) / len(data_list)
ZeroDivisionError: division by zero
show a Python call stack at the point the exception was raised
Python call stack
pdb - the Python debugger
interactive source code debugger
part of the Python's standard library
allows to stop and exammine running code
two main ways of using pdb:
start the program with the debugger: python -m pdb code.py
stop the program and invoke the debugger: import pdb; pdb.set_trace() or breakpoint() (in Python 3.7+)
using the interactive debugger
after inoking the debugger using breakpoint() in the code:
> /Users/dorota/teaching/pycon_debug/part2/standard_deviation.py(24)std_dev()
-> mean = sum(data_list) / N
(Pdb)
q - quit the debugger
c(ontinue) - continue execution until the next breakpoint
pdb commands: continue and step
n(ext) - continue execution until the next line in the current function
s(tep) - execute the current line, steps inside a called function
c(ontinue) - continue execution until the next breakpoint
b <line number, name of function> - adding new breakpoint
<enter> - repeating the last command
<upper arrow> - search the commands history
pdb commands: explore the code and variables
p - evaluate the expression and print its value
pp - like p, but uses the pprint module
l(ist) - list source code around the current line
ll - list all source code for the current function or frame
a(rgs) - print the argument list of the current function
pdb commands: check the stack trace
w(here) - print a stack trace, with the most recent frame at the bottom
u(p) - move the current frame one level up
d(own) - move the current frame one level down
Check full description of up / down and more commands here
pdb: variables listing
locals() - display all the local variables
globals() - display all the global variables
display var (pdb command) - track the changes of variable var