PDB: The Python Debugger

https://www.youtube.com/playlist?list=PLTejD6544296J48NA7-agj70__GVLpZ8J
playlist about this presentation
Johni Douglas Marangon
www.linkedin.com/in/johnidouglas
github.com/johnidm
johnidouglas.com
johni.douglas.marangon@gmail.com
Fullstack Developer at Contentools
suggestion for talks next
27. Debugging and Profiling
https://docs.python.org/3/library/debug.html
27.4. The Python Profilers
https://docs.python.org/3/library/profile.html
official documentation
27.3. pdb — The Python Debugger
https://docs.python.org/3/library/pdb.html
references
        
            

https://www.youtube.com/watch?v=P0pIW5tJrRM

https://www.youtube.com/watch?v=lnlZGhnULn4

https://www.youtube.com/watch?v=vfPtGsSJldg

http://www.onlamp.com/pub/a/python/2005/09/01/debugger.html
    
https://pymotw.com/2/pdb/
    

        
pdb
an interactive source code debugger for Python programs
invoking PDB
python -m pdb <filename>
script mode
trace mode
run mode
import pdb; pdb.set_trace()
>>> import pdb
>>> pdb.run('<module>.<func<args>>')
when reached, the debugger is started with the prompt "(Pdb)"
postmortem mode 
import pdb; pdb.pm()
try:
    # your code
except:
    import pdb, traceback, sys
    _, _, tb = sys.exc_info()
    traceback.print_exc()
    pdb.post_mortem(tb)
Enter post-mortem debugging of the given traceback object. If no traceback is given, it uses the one of the exception that is currently being handled (an exception must be being handled if the default is to be used).
basic
(h)elp | ? - list of available commands

(q)uit - quit from the debugger

(h)elp command - print help about command

<ENTER> - repeat the last command
examine code
p(rint) expr - print the value of expr

you can print multiple variables
p a, b, c
you cam omit p


pp expr - pretty print the value of expr


a(args) - print the argument list of the current function

 

 
navigate code
l(ist) – list 11 lines around the line
 + and – 5 around the line
The current line in the current frame is indicated by ->
With . as argument list lines around the current line


l(list) lineno - list starting lineno

 

l(list) startno, endno - list the given range

   
ll(longlist) - List all source code for the current function or frame
interactive code
n(ext) – step over next line of code

c(ont(inue)) - continue execution
s(tep) - step into function

r(eturn) - skip ahead to the 
end of the subroutine

until​ - skip over a loop

 

j(ump) lineno - set the next line that will be executed
traverse the stack
(w)here - print current locations on stack trace

 

u(p) [count] - move the current frame to up

d(own) [count] - move the current frame to down
manipulation
!stmt – A python (one-line) statement
you may change a variable value or call a function
you can omit !


declare new variables

import modules and call functions
breakpoints
b(reak) – Show list of breakpoints


b(reak) lineno – Set a breakpoint on line lineno


(cl)ear - Remove all breakpoints


(cl)ear bpno – Remove the breakpoint number in list (use b to see list of breakpoints)

enable bpno – enable the breakpoint number in list (use b to see list of breakpoints)


disable bpno –  dissalbe the breakpoint number in list (use b to see list of breakpoints)
tbreak lineno - set a temporary brackpoint
is removed when first hit


ignore bpno, counter - ingnore a breackpoint number in list ignore counter
resetting the ignore count to zero re‐ enables the breakpoint immediately ingnore 1, 0
b(reak) func - set a brackpoint at first line of func


b(reak) lineno, cond - set a brackpoint with a condicitonal


b(reak) file:lineno - set a brackpoint into file at line lineno
advanced commands 
condition bpno [condition] - set a new condition for the breakpoint

commands [bpno] - specify a list of commands for breakpoint number

alias - list all aliases

alias [name [command]] - create an alias

unalias name - delete the specified alias
Saving Configuration Settings
create a .pdbrc file in root directory

define aliases, commom brakpoints, etc
# Print instance variables (usage "pi classInst")
alias pi for k in %1.__dict__.keys(): print("%1.",k,"=",%1.__dict__[k])
# Print instance variables in self
alias ps pi self

# current user dir
alias lsd import os; print(os.path.abspath(os.path.expanduser('~')))
Other tools
rdb - http://docs.celeryproject.org/en/latest/tutorials/debugging.html

wdb - https://pypi.python.org/pypi/wdb/

ipdb - https://pypi.python.org/pypi/ipdb/

pdbpp - https://pypi.python.org/pypi/pdbpp/

rpdb - https://pypi.python.org/pypi/rpdb/

pydbgr - https://pypi.python.org/pypi/pydbgr/

pudb - https://pypi.python.org/pypi/pudb
thanks
questions? 

pdb the Python Debugger

By Johni Douglas Marangon

pdb the Python Debugger

  • 1,071