Skip to content

Instantly share code, notes, and snippets.

@obfusk
Last active November 10, 2025 05:32
Show Gist options
  • Select an option

  • Save obfusk/208597ccc64bf9b436ed to your computer and use it in GitHub Desktop.

Select an option

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()))
@bsima

bsima commented Aug 10, 2015

Copy link
Copy Markdown

I use this code snippet so often it's not even funny...

@zachaysan

Copy link
Copy Markdown

Haha, amazing. Anytime I'm stuck on these science projects that lack an iPython this is clutch.

@mach-kernel

Copy link
Copy Markdown

@obfusk is there any way to halt execution? Trying to use this in a complex Hadoop streaming script and it floods its own stdin with whatever was queued up for stdout.

I've been using either sleep or signal but I'm floored that there isn't a better way to do this.

@abevoelker

Copy link
Copy Markdown

If you want to be able to reference imported modules, try this variation:

import code; code.interact(local=dict(globals(), **locals()))

@mmcintyre123

Copy link
Copy Markdown

Brilliant! Thanks for posting.

@moritzbe

Copy link
Copy Markdown

thumbs up!

@adrianobarroso

Copy link
Copy Markdown

Great

@bcaccinolo

Copy link
Copy Markdown

pip install ipdb

import ipdb; ipdb.set_trace()

@telwell

telwell commented Feb 19, 2018

Copy link
Copy Markdown

Just wanted to say that I love this snippet ❤️

@ianmiell

Copy link
Copy Markdown

Me too <3

@reisner

reisner commented Jul 27, 2018

Copy link
Copy Markdown

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

@crajcan

crajcan commented Aug 19, 2018

Copy link
Copy Markdown

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

@cantorman

Copy link
Copy Markdown

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

@tasaif

tasaif commented Feb 7, 2019

Copy link
Copy Markdown

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

@charterchap

Copy link
Copy Markdown

@jecompton

Copy link
Copy Markdown

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

@moritzbe

Copy link
Copy Markdown

perfect! Its a standard in my work ;)

@vsuzdaltsev

Copy link
Copy Markdown

import ipdb; ipdb.set_trace()

danke!

@anahimana

Copy link
Copy Markdown

Amazing! Thank you!

@engr-hasanuzzaman

Copy link
Copy Markdown

It was very helpful to me.

@ppanchal97

Copy link
Copy Markdown

⭐ 👍

@hieuns-0318

Copy link
Copy Markdown

Very helpful, thank you 👍 !

@zuhrig

zuhrig commented Apr 8, 2021

Copy link
Copy Markdown

still searching for this
jjajaja

@TimB0

TimB0 commented May 27, 2021

Copy link
Copy Markdown

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

@obfusk

obfusk commented Jun 4, 2021

Copy link
Copy Markdown
Author

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

charterchap commented Sep 3, 2021

Copy link
Copy Markdown
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

Victorcorcos commented Sep 2, 2024

Copy link
Copy Markdown

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