在节目中跳入 Python 交互式会话?

发布于 2024-07-22 00:52:38 字数 486 浏览 1 评论 0原文

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

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

我认为这会非常方便;)

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

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

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

温柔嚣张 2024-07-29 00:52:38

您可以使用 code.InteractiveConsole 跳转到交互式会话,如此处; 但是我不知道如何从 Eclipse 触发这个。

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

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.

并安 2024-07-29 00:52:38

我长期以来一直在我的 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

这是来源有关 StackOverflow 的更多讨论

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.

不一样的天空 2024-07-29 00:52:38

因此,距 OP 的问题大约一年后,PyDev 内置了此功能。我不确定何时引入此功能,但我所知道的是我花了最后约 2 小时谷歌搜索...配置 iPython 等(其中看起来它可以完成这项工作),但只是意识到 Eclipse/PyDev 拥有我想要的 ootb。

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

很棒的东西,请参阅 http://pydev.org/manual_adv_debug_console.html 了解更多详细信息。

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.

浪漫人生路 2024-07-29 00:52:38

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

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()
意犹 2024-07-29 00:52:38

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文