ipdb is an interactive Python Debugger with IPython integration, which features tab completion and syntax highlighting, etc. In layman's terms, ipdb is a better pdb.
ref:
https://github.com/gotcha/ipdb
Usage
$ pip install -U ipdb
ref:
https://pypi.python.org/pypi/ipdb
Add a breakpoint to any place you want to inspect, then run your code.
import ipdb; ipdb.set_trace()
If you use Sublime Text 3, try Python Breakpoints.
https://github.com/obormot/PythonBreakpoints
Useful Commands
Oldest frame is the frame in the stack where your program started; it is the oldest in time; the Newest frame, the other end of the stack, is where Python is executing code and is the current frame of execution.
# help: Print the list of all commands
h
# help: Print help about the certain command
h break
# print: Print the value of the expression
p some_obj
pp some_obj
# Print detailed information about the object
pinfo some_obj
pinfo2 some_obj
# args: Print arguments with their values of the current function
a
# list: List 11 lines of source code around the current line
l
# list: List 11 lines of source code around line 123
l 123
# longlist: List all source code for the current function or frame
ll
# jump: Jump to line 123, skip the execution of anything between
j 123
# args: List all arguments of the current function
a
# step: Execute code line by line, it may jump to another frame when a function call is encountered
s
# next: Execute code line by line, it doesn't enter functions called from the statement being executed
n
# return: Continue execution until the current function returns.
r
# continue: Continue execution, only stop when a breakpoint is encountered
c
# break: List all breakpoints
b
# break: Set a breakpoint at line 123
b 123
# break: Set a breakpoint at line 123 of file.py
b path/to/file.py:123
# break: Set a breakpoint on some_func that will be triggered if some_arg == 0
b some_func, some_arg == 0
# clear: Clear all breakpoints
clear
# where: Print a stack trace
w
# up: Move the current frame one level up in the stack trace
u
# down: Move the current frame one level down in the stack trace
d
# quit: Quit debugging
q
# use ! to run Python code that may conflict with pdb's built-in commands
!r = 123
!r = 123; c = 455
ref:
https://docs.python.org/2/library/pdb.html#debugger-commands
https://docs.python.org/3/library/pdb.html#debugger-commands
https://pymotw.com/2/pdb/
https://pymotw.com/3/pdb/
https://medium.com/instamojo-matters/become-a-pdb-power-user-e3fc4e2774b2
post_mortem
Debugging a failure after a program terminates is called post-mortem debugging.
>>> do_shit(a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "pdb_post_mortem.py", line 13, in go
for i in range(self.num_loops):
AttributeError: 'MyObj' object has no attribute 'num_loops'
>>> import ipdb; ipdb.pm()
>>> w
trace
Tracing a program as it runs. In this case, it will enter ipdb when sys.path
changes.
import sys
# this function will execute on every line!!!
def trace_sys_path(frame, event, arg):
if sys.path[0].endswith('/lib'):
ipdb.set_trace()
return trace_sys_path
sys.settrace(trace_sys_path)
ref:
https://youtu.be/5XvAVgcbmdY?t=22m51s
Use IPython magic functions in ipdb
Because that ipdb is not a full IPython shell: actually, it provides the same Python Debugger interface as pdb, ipdb lacks many features of IPython, for instance, magic functions. You could use following code to enter a real IPython environment for debugging.
from IPython import embed; embed()
Instead of import ipdb; ipdb.set_trace()
.
ref:
http://stackoverflow.com/questions/16184487/use-ipython-magic-functions-in-ipdb-shell
https://github.com/gotcha/ipdb/issues/33