python 中交互式调试的最佳方法是什么?

发布于 2024-09-11 03:34:25 字数 582 浏览 3 评论 0原文

我想利用 python 的自省功能进行调试/开发,但找不到合适的工具。

我需要在特定位置或特定事件(如异常)进入 shell(例如 IPython),并将 shell 的局部变量和全局变量设置为框架的局部变量和全局变量。

我自己的快速破解来说明它:

import inspect
from IPython.Shell import IPShellEmbed
def run_debug():
    stack = inspect.stack()
    frame = stack[1][0]
    loc = frame.f_locals
    glob = frame.f_globals
    shell = IPShellEmbed()
    shell(local_ns=loc, global_ns=glob)

根据“断点”或 try/ except 的 run_debug() 调用。但是,显然,这需要大量的改进,尤其是要正确地与线程应用程序一起使用。

winpdb 在控制台上有断点,但我发现无法从中快速运行正确的 python shell,并且 eval()/exec() 对于长时间调试来说不是很方便。

I want to utilize introspection capability of python for debugging/development, but cannot find appropriate tool for this.

I need to enter into shell (IPython for example) at specific position or at specific event (like exception), with locals and globals of shell being set to the frame's ones.

My own quick hack to illustrate it:

import inspect
from IPython.Shell import IPShellEmbed
def run_debug():
    stack = inspect.stack()
    frame = stack[1][0]
    loc = frame.f_locals
    glob = frame.f_globals
    shell = IPShellEmbed()
    shell(local_ns=loc, global_ns=glob)

With according run_debug() call from 'breakpoint' or try/except. But, obviously, this needs alot of polishing, esp to work with threaded apps properly.

winpdb has breakpoints with console, but I found no way to quickly run proper python shell from it, and eval()/exec() are not very handy for long debug.

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

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

发布评论

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

评论(3

墨落画卷 2024-09-18 03:34:25

与您已经在做的事情类似,有 ipdb。实际上,它是带有 ipython shell 的 pdb(即制表符补全、所有各种魔法函数等)。

它实际上正在执行您在问题中发布的小代码片段的操作,但将其包装到一个简单的“ipdb.set_trace()”调用中。

Similar to what you're already doing, there's ipdb. Effectively, it's pdb with ipython's shell (i.e. tab completion, all the various magic functions, etc).

It's actually doing exactly what the little code snipped you posted in your question does, but wraps it into a simple "ipdb.set_trace()" call.

送君千里 2024-09-18 03:34:25

出于个人/教育目的,您可以使用 WingIDE - 它们具有一些非常可靠的调试功能。

当然,如果您只是担心更改值,您始终可以使用 raw_input() - 但这可能不够先进,无法满足您的需求。

For personal/education purposes you can use WingIDE - they have some pretty solid debugging capabilities.

Of course if you're just worried about changing values you can always just use raw_input() - but that may not be advanced enough for your needs.

苦妄 2024-09-18 03:34:25

如果您从 ipython 运行代码并遇到异常,则可以随后调用 %debug 以在异常处放入 pdb。这应该会给你你想要的。或者,如果您运行 ipython -pdb,当发生未捕获的异常时,您将自动进入 pdb。

If you run your code from ipython and hit an exception, you can call %debug afterwards to drop into a pdb at the exception. This should give you what you want. Or if you run ipython -pdb, you will automatically be dropped into pdb when an uncaught exception occurs.

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