Debugger commands
h
(help) Display command listhelp command
Show documentation for commandc
(continue) Resume program executionq
(quit) Exit debugger without executing any more codeb
(break) number Set breakpoint at number in current fileb path/to/file.py:number
Set breakpoint at line number in specified files
(step) Step into function calln
(next) Execute current line and advance to next line at current levelu
/d
(up) / (down) Move up/down in function call stacka
(args) Show arguments for current functiondebug statement
Invoke statement statement in new (recursive) debuggerl
(list) statement Show current position and context at current level of stackw
(where) Print full stack trace with context at current position
Post-mortem debugging
%debug
Entering %debug
immediately after an exception has occurred drops you into the stack frame where the exception was raisedUtility functions
Poor man’s breakpointdef set_trace():
from IPython.core.debugger import Pdb
Pdb(color_scheme='Linux').set_trace(sys._getframe().f_back)
Putting set_trace()
in your code will automatically drop into the debugger when the line is executed.Interactive function debugging
def debug(f, *args, **kwargs):
from IPython.core.debugger import Pdb
pdb = Pdb(color_scheme='Linux')
return pdb.runcall(f, *args, **kwargs)
Passing a function to debug
will drop you into the debugger for an arbitrary function call. debug(fn, arg1, arg2, arg3, kwarg=foo, kwarg=bar)
Interactive script debugging
Executing a script via%run
with -d
will start the script in the debugger%run -d ./my_script.py
Specifying a line number with -b
starts the script with a breakpoint already set%run -d -b20 ./my_script.py # sets a breakpoint on line 20
Taken from Python for Data Anlysis by Wes McKinney
No comments:
Post a Comment