Jiaaro
Jiaaro

Reputation: 76898

Jump into a Python Interactive Session mid-program?

Hey I was wondering... I am using the pydev with eclipse and I'm really enjoying the powerful debugging features, but I was wondering:

Is it possible to set a breakpoint in eclipse and jump into the interactive python interpreter during execution?

I think that would be pretty handy ;)

edit: I want to emphasize that my goal is not to jump into a debugger. pydev/eclipse have a great debugger, and I can just look at the traceback and set break points.

What I want is to execute a script and jump into an interactive python interpreter during execution so I can do things like...

I know you can do this all with a debugger, but I can do it faster in the interactive interpreter because I can try something, see that it didn't work, and try something else without having get the app back to the point of executing that code again.

Upvotes: 13

Views: 3408

Answers (5)

Darren Bishop
Darren Bishop

Reputation: 2585

So roughly a year on from the OP's question, PyDev has this capability built in. I am not sure when this feature was introduced, but all I know is I've spent the last ~2hrs Googling... configuring iPython and whatever (which was looking like it would have done the job), but only to realise Eclipse/PyDev has what I want ootb.

As soon as you hit a breakpoint in debug mode, the console is right there ready and waiting! I only didn't notice this as there is no prompt or blinking cursor; I had wrongly assumed it was a standard, output-only, console... but it's not. It even has code-completion.

Great stuff, see http://pydev.org/manual_adv_debug_console.html for more details.

Upvotes: 9

rz.
rz.

Reputation: 20037

This is from an old project, and I didn't write it, but it does something similar to what you want using ipython.

'''Start an IPython shell (for debugging) with current environment.                    
Runs Call db() to start a shell, e.g.                                                  


def foo(bar):                                                                          
    for x in bar:                                                                      
        if baz(x):                                                                     
            import ipydb; ipydb.db() # <-- start IPython here, with current value of x (ipydb is the name of this module).
.                                                                                      
'''
import inspect,IPython

def db():
    '''Start IPython shell with callers environment.'''
    # find callers                                                                     
    __up_frame = inspect.currentframe().f_back
    eval('IPython.Shell.IPShellEmbed([])()', # Empty list arg is                       
         # ipythons argv later args to dict take precedence, so                        
         # f_globals() shadows globals().  Need globals() for IPython                  
         # module.                                                                     
         dict(globals().items() + __up_frame.f_globals.items()),
         __up_frame.f_locals)

edit by Jim Robert (question owner): If you place the above code into a file called my_debug.py for the sake of this example. Then place that file in your python path, and you can insert the following lines anywhere in your code to jump into a debugger (as long as you execute from a shell):

import my_debug
my_debug.db()

Upvotes: 6

Schnouki
Schnouki

Reputation: 7707

You can jump into an interactive session using code.InteractiveConsole as described here; however I don't know how to trigger this from Eclipse.

A solution might be to intercept Ctrl+C to jump into this interactive console (using the signal module: signal.signal(signal.SIGINT, my_handler)), but it would probably change the execution context and you probably don't want this.

Upvotes: 2

Michael Kuhn
Michael Kuhn

Reputation: 8972

I've long been using this code in my sitecustomize.py to start a debugger on an exception. This can also be triggered by Ctrl+C. It works beautifully in the shell, don't know about eclipse.

import sys

def info(exception_type, value, tb):
   if hasattr(sys, 'ps1') or not sys.stderr.isatty() or not sys.stdin.isatty() or not sys.stdout.isatty() or type==SyntaxError:
      # we are in interactive mode or we don't have a tty-like
      # device, so we call the default hook
      sys.__excepthook__(exception_type, value, tb)
   else:
      import traceback
      import pdb


      if exception_type != KeyboardInterrupt:
          try:
              import growlnotify
              growlnotify.growlNotify("Script crashed", sticky = False)
          except ImportError:
              pass

      # we are NOT in interactive mode, print the exception...
      traceback.print_exception(exception_type, value, tb)
      print
      # ...then start the debugger in post-mortem mode.
      pdb.pm()

sys.excepthook = info

Here's the source and more discussion on StackOverflow.

Upvotes: 5

Salim Fadhley
Salim Fadhley

Reputation: 23008

If you are already running in debug mode you can set an additional breakpoint if the program execution is currently paused (e.g. because you are already at a breakpoint). I just tried it out now with the latest Pydev - it works just fine.

If you are running normally (i.e. not in debug mode) all breakpoints will be ignored. No changes to breakpoints will alter the way a non-debug run works.

Upvotes: -2

Related Questions