Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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...

  • poke around
  • check the values of things
  • manipulate variables
  • figure out some code before I add it to the app

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.

like image 831
Jiaaro Avatar asked May 29 '09 12:05

Jiaaro


People also ask

How do I go into interactive mode in Python?

On Windows, bring up the command prompt and type "py", or start an interactive Python session by selecting "Python (command line)", "IDLE", or similar program from the task bar / app menu. IDLE is a GUI which includes both an interactive mode and options to edit and run files.

How to get out of debug Python?

The Python debugger will automatically start over when it reaches the end of your program. Whenever you want to leave the pdb console, type the command quit or exit .

How to use a debugger Python?

If you're only interested in debugging a Python script, the simplest way is to select the down-arrow next to the run button on the editor and select Debug Python File in Terminal.

What is Python interactive mode?

Interactive mode is very convenient for writing very short lines of code. In python it is also known as REPL which stands for Read Evaluate Print Loop. Here, the read function reads the input from the user and stores it in memory. Eval function evaluates the input to get the desired output.


2 Answers

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.

like image 55
Darren Bishop Avatar answered Sep 23 '22 21:09

Darren Bishop


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()
like image 21
rz. Avatar answered Sep 23 '22 21:09

rz.