36

I often have the case that I'll be writing a script, and I'm up to a part of the script where I want to play around with some of the variables interactively. Getting to that part requires running a large part of the script I've already written.

In this case it isn't trivial to run this program from inside the shell. I would have to recreate the conditions of that function somehow.

What I want to do is call a function, like runshell(), which will run the python shell at that point in the program, keeping all variables in scope, allowing me to poke around in it.

How would I go about doing that?

asked Oct 6, 2011 at 16:15
3
  • Are you talking about using the Python debugger? Or are you looking for design advice on how to decompose a large script into more manageable pieces? Commented Oct 6, 2011 at 16:20
  • i'm asking about the former - i had forgotten that word ('debugger')! Commented Oct 6, 2011 at 16:49
  • possible duplicate of Jump into a Python Interactive Session mid-program? Commented Oct 3, 2013 at 2:28

5 Answers 5

57
import code
code.interact(local=locals())

But using the Python debugger is probably more what you want:

import pdb
pdb.set_trace()
answered Oct 6, 2011 at 16:21
Sign up to request clarification or add additional context in comments.

3 Comments

thanks! I am looking for this for a while! import code
If you're at a breakpoint in a PDB session, and you want to descend into an interactive Python shell from there, the import code; code.interact(local=locals()) thing is perfect for that. Variables and functions defined in that interactive shell are then available when you hit ^D and return to the debugging session. Thanks very much, Michael Hoffman.
It works but does not support TAB auto-completion.
24

By far the most convenient method that I have found is:

import IPython
IPython.embed()

You get all your global and local variables and all the creature comforts of IPython: tab completion, auto indenting, etc.

You have to install the IPython module to use it of course:

pip install ipython
David Ferenczy Rogožan
25.8k12 gold badges88 silver badges73 bronze badges
answered Nov 11, 2013 at 14:15

1 Comment

I love this with one caveat. I have to redo my imports. Any way to pass through the the imports to the new interactive shell as well?
7

For practicality I'd like to add that you can put the debugger trace in a one liner:

import pdb; pdb.set_trace()

Which is a nice line to add to an editor that supports snippets, like TextMate or Vim+SnipMate. I have it set up to expand "break" into the above one liner.

answered Jan 18, 2013 at 1:00

1 Comment

yep this is indeed what I end up doing nowadays
6

You can use the python debugger (pdb) set_trace function.

For example, if you invoke a script like this:

def whatever():
 x = 3
 import pdb
 pdb.set_trace()
if __name__ == '__main__':
 whatever()

You get the scope at the point when set_trace is called:

$ python ~/test/test.py
--Return--
> /home/jterrace/test/test.py(52)whatever()->None
-> pdb.set_trace()
(Pdb) x
3
(Pdb) 
answered Oct 6, 2011 at 16:21

Comments

0

Not exactly a perfect source but I've written a few manhole's before, here is one I wrote for an abandoned pet project http://code.google.com/p/devdave/source/browse/pymethius/trunk/webmud/handlers/konsole.py

And here is one from the Twisted Library http://twistedmatrix.com/trac/browser/tags/releases/twisted-8.1.0/twisted/manhole/telnet.py the console logic is in Shell.doCommand

answered Oct 6, 2011 at 16:23

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.