Jump into a Python Interactive Session mid-program?

后端 未结 5 1282
陌清茗
陌清茗 2020-12-23 15:35

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

相关标签:
5条回答
  • 2020-12-23 16:00

    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()
    
    0 讨论(0)
  • 2020-12-23 16:06

    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.

    0 讨论(0)
  • 2020-12-23 16:07

    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.

    0 讨论(0)
  • 2020-12-23 16:09

    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.

    0 讨论(0)
  • 2020-12-23 16:10

    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.

    0 讨论(0)
提交回复
热议问题