Skip to content

Instantly share code, notes, and snippets.

@obfusk
Last active September 2, 2024 16:23
Show Gist options
  • Save obfusk/208597ccc64bf9b436ed to your computer and use it in GitHub Desktop.
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
import code; code.interact(local=dict(globals(), **locals()))
@adrianobarroso
Copy link

Great

@bcaccinolo
Copy link

pip install ipdb

import ipdb; ipdb.set_trace()

@telwell
Copy link

telwell commented Feb 19, 2018

Just wanted to say that I love this snippet ❤️

@ianmiell
Copy link

Me too <3

@reisner
Copy link

reisner commented Jul 27, 2018

The best thing about this snippet is how intuitive it is.

@crajcan
Copy link

crajcan commented Aug 19, 2018

How do I continue execution when I want to exit the interpreter?

@cantorman
Copy link

@crajcan you probably figured this out, so for those who come later, ^D seems to continue execution.

@tasaif
Copy link

tasaif commented Feb 7, 2019

I wonder if maybe this should be saved somewhere besides a gist

@charterchap
Copy link

@jecompton
Copy link

@charterchap No. ^D continues execution, exit() terminates completely.

@moritzbe
Copy link

perfect! Its a standard in my work ;)

@vsuzdaltsev
Copy link

import ipdb; ipdb.set_trace()

danke!

@anahimana
Copy link

Amazing! Thank you!

@engr-hasanuzzaman
Copy link

It was very helpful to me.

@ppanchal97
Copy link

⭐ 👍

@hieuns-0318
Copy link

Very helpful, thank you 👍 !

@zuhrig
Copy link

zuhrig commented Apr 8, 2021

still searching for this
jjajaja

@TimB0
Copy link

TimB0 commented May 27, 2021

i dont see how to use this...how do i view the data in my object? is it the same commands as pdb?

@obfusk
Copy link
Author

obfusk commented Jun 4, 2021

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)

@charterchap
Copy link

charterchap commented Sep 3, 2021

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

@Victorcorcos
Copy link

Victorcorcos commented Sep 2, 2024

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 or continue: Continue execution until the next breakpoint or the end of the program.
  • n or next: Continue execution until the next line in the current function. Does not step into functions.
  • s or step: Step into the function called at the current line or go to the next line in the current function.
  • r or return: Continue execution until the current function returns.
  • q or quit: Exit the debugger and stop the program.

Inspecting Code

  • l or list: 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 or where: Print a stack trace, showing the call stack of the current execution point.
  • u or up: Move up one level in the stack trace (closer to the start of the program).
  • d or down: Move down one level in the stack trace (closer to where the program is currently executing).

Breakpoints

  • b or break: 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 or clear: 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 or print: Evaluate and print the value of an expression, e.g., print x.
  • pp: Pretty-print the value of an expression.
  • a or args: 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 set x to 42.
  • retval: Print the return value from the last executed function.

Other Commands

  • h or help: 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 using q.
  • 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 of y.
  • 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