【问题标题】:Jump into a Python Interactive Session mid-program?跳入 Python 交互式会话中程?
【发布时间】:2010-10-29 21:02:31
【问题描述】:

嘿,我想知道...我正在使用 pydev 和 eclipse,我真的很喜欢强大的调试功能,但我想知道:

是否可以在eclipse中设置断点,在执行过程中跳转到交互式python解释器中?

我认为这会很方便;)

edit:我想强调我的目标不是跳入调试器。 pydev/eclipse 有一个很棒的调试器,我可以只看回溯并设置断点。

我想要的是执行一个脚本并在执行过程中跳转到一个交互式 python 解释器,这样我就可以做一些事情......

  • 四处逛逛
  • 检查事物的价值
  • 操作变量
  • 在我将代码添加到应用程序之前找出一些代码

我知道您可以使用调试器完成这一切,但我可​​以在交互式解释器中更快地完成这一切,因为我可以尝试一些东西,看到它不起作用,然后尝试其他东西,而无需让应用程序返回再次执行该代码。

【问题讨论】:

  • 不需要强调 - 考虑重新格式化?

标签: python eclipse debugging breakpoints pydev


【解决方案1】:

距 OP 的问题大约一年后,PyDev 内置了此功能。我不确定何时引入此功能,但我所知道的是我在过去的 ~2 小时谷歌搜索...配置 iPython 和随便什么(看起来它可以完成这项工作),但只是为了意识到 Eclipse/PyDev 有我想要的 ootb。

一旦您在调试模式下遇到断点,控制台就在那里准备就绪并等待! 我只是没有注意到这一点,因为没有提示或闪烁的光标;我错误地认为它是一个标准的、仅输出的控制台……但事实并非如此。它甚至具有代码完成功能。

好东西,更多详情请见http://pydev.org/manual_adv_debug_console.html

【讨论】:

  • 是的,您甚至可以在 Apache + mod_wsgi 堆栈(或其他生产设置)上使用它。但是,为了确保交互式控制台正常工作,您必须这样启动它:from pydevsrc import pydevd;pydevd.settrace('<YOUR-IP>', stdoutToServer=True, stderrToServer=True)
【解决方案2】:

这是来自一个旧项目,不是我写的,但它的功能类似于您使用 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)

由 Jim Robert(问题所有者)编辑:如果您为了本示例将上述代码放入名为 my_debug.py 的文件中。然后将该文件放在您的python路径中,您可以在代码中的任何位置插入以下行以跳转到调试器(只要您从shell执行):

import my_debug
my_debug.db()

【讨论】:

    【解决方案3】:

    我长期以来一直在我的sitecustomize.py 中使用此代码来启动异常调试器。这也可以通过 Ctrl+C 触发。它在 shell 中工作得很好,不知道 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
    

    这是sourcemore discussion on StackOverflow

    【讨论】:

      【解决方案4】:

      您可以使用code.InteractiveConsole 跳转到交互式会话,如here 所述;但是我不知道如何从 Eclipse 中触发它。

      一种解决方案可能是拦截 Ctrl+C 以跳转到此交互式控制台(使用 signal 模块:signal.signal(signal.SIGINT, my_handler)),但它可能会更改执行上下文,您可能不希望这样。

      【讨论】:

        【解决方案5】:

        如果您已经在调试模式下运行,如果程序执行当前暂停(例如,因为您已经在断点处),您可以设置一个额外的断点。我现在刚刚使用最新的 Pydev 进行了尝试——它工作得很好。

        如果您正常运行(即未处于调试模式),所有断点都将被忽略。对断点的任何更改都不会改变非调试运行的工作方式。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-06-17
          • 2010-10-31
          • 2019-02-15
          • 1970-01-01
          • 1970-01-01
          • 2012-10-18
          • 2014-02-05
          • 1970-01-01
          相关资源
          最近更新 更多