-
-
Save obfusk/208597ccc64bf9b436ed to your computer and use it in GitHub Desktop.
import code; code.interact(local=dict(globals(), **locals())) |
Brilliant! Thanks for posting.
thumbs up!
Great
pip install ipdb
import ipdb; ipdb.set_trace()
Just wanted to say that I love this snippet ❤️
Me too <3
The best thing about this snippet is how intuitive it is.
How do I continue execution when I want to exit the interpreter?
@crajcan you probably figured this out, so for those who come later, ^D seems to continue execution.
I wonder if maybe this should be saved somewhere besides a gist
@charterchap No. ^D continues execution, exit() terminates completely.
perfect! Its a standard in my work ;)
import ipdb; ipdb.set_trace()
danke!
Amazing! Thank you!
It was very helpful to me.
⭐ 👍
Very helpful, thank you 👍 !
still searching for this
jjajaja
i dont see how to use this...how do i view the data in my object? is it the same commands as pdb?
i dont see how to use this...how do i view the data in my object? is it the same commands as pdb?
@TimB0 The same way you would in an interactive Python shell :)
$ python3 -c 'x = 42; import code; code.interact(local=dict(globals(), **locals()))'
>>> x
42
(and when you're done, use ^D
(EOF) to continue or quit()
to abort the program)
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.
If you want to be able to reference imported modules, try this variation: