Created for
pdb
- The Python Debuggerpdb
- The Python Debuggerpdb
- The Python Debugger
import pdb; pdb.set_trace()
at the location you want to break into the debugger. This is useful to hard-code a breakpoint at a given point in a program
python3 -m pdb myscript.py
def sum_numbers(num_range):
suma = 0
for i in num_range:
suma =+ i
#enter debug mode and set a breakpoint
import pdb; pdb.set_trace()
return suma
def main():
print( sum_numbers(range(1,10)) )
if __name__ == '__main__':
main()
$ python3 debug_demo.py
> /tmp/examples/debug_demo.py(3)sum_numbers()
-> for i in num_range:
(Pdb)
Command | Shortcut | Action |
---|---|---|
help | ? | Without argument, print the list of available commands. With a command name as argument, print help about that command. |
list | l | Displays 11 lines around the current line or continue the previous listing. |
long list | ll | In Python 3.2 and above, shows the source code for the current function or frame |
Command | Shortcut | Action |
---|---|---|
step | s | Execute the current line, stop at the first possible occasion. |
next | n | Continue execution until the next line in the current function is reached or it returns |
break | b | Without argument, list all breaks. A line number or a function name can be passed as first argument. As second argument can be passed an expression which must evaluate to true before the breakpoint is honoured. |
return | r | Continue execution until the current function returns. |
$ python3 -m pdb debug_demo.py
(Pdb) ?
Documented commands (type help <topic>):
========================================
EOF c d h list q rv undisplay
a cl debug help ll quit s unt
alias clear disable ignore longlist r source until
args commands display interact n restart step up
b condition down j next return tbreak w
break cont enable jump p retval u whatis
bt continue exit l pp run unalias where
Miscellaneous help topics:
==========================
pdb exec
(Pdb) help break
b(reak) [ ([filename:]lineno | function) [, condition] ]
Without argument, list all breaks.
With a line number argument, set a break at this line in the
current file. With a function name, set a break at the first
executable line of that function. If a second argument is
present, it is a string specifying an expression which must
evaluate to true before the breakpoint is honored.
The line number may be prefixed with a filename and a colon,
to specify a breakpoint in another file (probably one that
hasn't been loaded yet). The file is searched for on
sys.path; the .py suffix may be omitted.
(Pdb)
(Pdb) ll
1 -> def sum_numbers(num_range):
2 suma = 0
3 for i in num_range:
4 suma =+ i
5 # import pdb; pdb.set_trace()
6
7 return suma
8
9 def main():
10 print( sum_numbers(range(1,10)) )
11
12 if __name__ == '__main__':
13 main()
(Pdb)
(Pdb) b 4
Breakpoint 1 at /tmp/examples/debug_demo.py:4
(Pdb)
(Pdb) c
> /tmp/examples/debug_demo.py(4)sum_numbers()
-> suma =+ i
(Pdb)
(Pdb) p suma
3
(Pdb) p suma, i
(3, 4)
# list breakpoints:
(Pdb) b
Num Type Disp Enb Where
1 breakpoint keep yes at /tmp/examples/debug_demo.py:4
#set commands to be executed on breakpoint 1:
(Pdb) commands 1
(com) p suma, i
(com) end
# continue to breakpoint and look that commands was executed
(Pdb) c
(0, 1)
launch.json
Command | Keyboard Shortcut |
---|---|
Continue / Pause | F5 |
Step Over | F10 |
Step Into | F11 |
Step Out | Shift+F11 |
Restart | Ctrl+Shift+F5 |
Stop | Shift+F5 |
These slides are based on
customised version of
framework