python 中交互式调试的最佳方法是什么?
我想利用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
与您已经在做的事情类似,有 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.
出于个人/教育目的,您可以使用 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.如果您从 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.