Last active
December 7, 2024 13:12
-
-
Save obfusk/208597ccc64bf9b436ed to your computer and use it in GitHub Desktop.
python "breakpoint" (more or less equivalent to ruby's binding.pry); for a proper debugger, use https://docs.python.org/3/library/pdb.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import code; code.interact(local=dict(globals(), **locals())) |
def pry():
import inspect
frame = inspect.currentframe().f_back
try:
import code;
code.interact(local=dict(frame.f_globals, **frame.f_locals))
finally:
del frame
There are much better solutions, such as:
import ipdb; ipdb.set_trace()
or
import pdb; pdb.set_trace()
When running pdb
, the Python Debugger, you have access to a variety of commands (often referred to as "methods" in the context of interactive debuggers) that allow you to control the execution of your program, inspect variables, and navigate through your code. Below is a list of the most commonly used pdb
commands:
Control Execution
c
orcontinue
: Continue execution until the next breakpoint or the end of the program.n
ornext
: Continue execution until the next line in the current function. Does not step into functions.s
orstep
: Step into the function called at the current line or go to the next line in the current function.r
orreturn
: Continue execution until the current function returns.q
orquit
: Exit the debugger and stop the program.
Inspecting Code
l
orlist
: Display the source code around the current line. You can provide a range, e.g.,list 10, 20
to list lines 10 to 20.w
orwhere
: Print a stack trace, showing the call stack of the current execution point.u
orup
: Move up one level in the stack trace (closer to the start of the program).d
ordown
: Move down one level in the stack trace (closer to where the program is currently executing).
Breakpoints
b
orbreak
: Set a breakpoint at a specified line, e.g.,break 15
, or in a specified function, e.g.,break my_function
.tbreak
: Set a temporary breakpoint that will be removed once hit.cl
orclear
: Clear a breakpoint. You can specify a breakpoint number or clear all breakpoints.disable
: Disable a breakpoint without removing it.enable
: Enable a disabled breakpoint.
Inspecting Variables
p
orprint
: Evaluate and print the value of an expression, e.g.,print x
.pp
: Pretty-print the value of an expression.a
orargs
: Print the arguments of the current function.whatis
: Print the type of an expression.
Modifying Code
!
: Execute the Python statement, e.g.,!x = 42
to setx
to42
.retval
: Print the return value from the last executed function.
Other Commands
h
orhelp
: Show a list of available commands or help for a specific command, e.g.,help break
.pdb
: Re-enter the pdb prompt if you accidentally quit it usingq
.run
: Restart the program with the command line you gave.
Example Usage
Here’s an example of how some of these commands might be used in a debugging session:
import pdb
def my_function(x):
y = x + 2
pdb.set_trace() # Start the debugger here
z = y * 3
return z
my_function(10)
During the debugging session:
- You could use
n
to go to the next line (z = y * 3
). - Use
p y
to print the value ofy
. - Use
c
to continue running the program.
Summary
pdb
provides a comprehensive set of commands for controlling program execution, inspecting variables, setting breakpoints, and navigating the call stack, making it a powerful tool for debugging Python code.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@TimB0 The same way you would in an interactive Python shell :)
(and when you're done, use
^D
(EOF) to continue orquit()
to abort the program)