Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@bsima bsima commented Aug 10, 2015

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

@zachaysan

This comment has been minimized.

Copy link

@zachaysan zachaysan commented Sep 17, 2015

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

@mach-kernel

This comment has been minimized.

Copy link

@mach-kernel mach-kernel commented Mar 9, 2016

@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

This comment has been minimized.

Copy link

@abevoelker abevoelker commented Apr 25, 2016

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

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

This comment has been minimized.

Copy link

@mmcintyre123 mmcintyre123 commented Dec 1, 2016

Brilliant! Thanks for posting.

@moritzbe

This comment has been minimized.

Copy link

@moritzbe moritzbe commented Mar 23, 2017

thumbs up!

@adrianobarroso

This comment has been minimized.

Copy link

@adrianobarroso adrianobarroso commented Jul 29, 2017

Great

@bcaccinolo

This comment has been minimized.

Copy link

@bcaccinolo bcaccinolo commented Aug 5, 2017

pip install ipdb

import ipdb; ipdb.set_trace()
@telwell

This comment has been minimized.

Copy link

@telwell telwell commented Feb 19, 2018

Just wanted to say that I love this snippet ❤️

@ianmiell

This comment has been minimized.

Copy link

@ianmiell ianmiell commented May 16, 2018

Me too <3

@reisner

This comment has been minimized.

Copy link

@reisner reisner commented Jul 27, 2018

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

@crajcan

This comment has been minimized.

Copy link

@crajcan crajcan commented Aug 19, 2018

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

@cantorman

This comment has been minimized.

Copy link

@cantorman cantorman commented Sep 29, 2018

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

@tasaif

This comment has been minimized.

Copy link

@tasaif tasaif commented Feb 7, 2019

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

@charterchap

This comment has been minimized.

Copy link

@charterchap charterchap commented Aug 8, 2019

@jecompton

This comment has been minimized.

Copy link

@jecompton jecompton commented Jan 14, 2020

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

@moritzbe

This comment has been minimized.

Copy link

@moritzbe moritzbe commented May 29, 2020

perfect! Its a standard in my work ;)

@vsuzdaltsev

This comment has been minimized.

Copy link

@vsuzdaltsev vsuzdaltsev commented Sep 8, 2020

import ipdb; ipdb.set_trace()

danke!

@anahimana

This comment has been minimized.

Copy link

@anahimana anahimana commented Oct 12, 2020

Amazing! Thank you!

@engr-hasanuzzaman

This comment has been minimized.

Copy link

@engr-hasanuzzaman engr-hasanuzzaman commented Jan 16, 2021

It was very helpful to me.

@ppanchal97

This comment has been minimized.

Copy link

@ppanchal97 ppanchal97 commented Mar 3, 2021

👍

@hieuns-0318

This comment has been minimized.

Copy link

@hieuns-0318 hieuns-0318 commented Mar 23, 2021

Very helpful, thank you 👍 !

@zuhrig

This comment has been minimized.

Copy link

@zuhrig zuhrig commented Apr 8, 2021

still searching for this
jjajaja

@TimB0

This comment has been minimized.

Copy link

@TimB0 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

This comment has been minimized.

Copy link
Owner Author

@obfusk 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

This comment has been minimized.

Copy link

@charterchap 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment