如何在Python自己的调试器(PDB)中执行多行语句 [英] How to execute multi-line statements within Python's own debugger (PDB)

查看:418
本文介绍了如何在Python自己的调试器(PDB)中执行多行语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在运行一个Python脚本,在其中编写以下代码来调用Python的调试器PDB:

So I am running a Python script within which I am calling Python's debugger, PDB by writing:

import ipdb; ipdb.set_trace()

(iPython的PDB版本,尽管就此而言,我认为这没有什么区别;我仅将其用于彩色输出).

(iPython's version of PDB, though for the matter I don't think it makes a difference; I use it for the colored output only).

现在,当我进入调试器时,我想执行一个多行语句,例如if子句或for循环,但是只要我键入

Now, when I get to the debugger I want to execute a multi-line statement such as an if clause or a for loop but as soon as I type

if condition:

并按回车键,我收到错误消息*** SyntaxError: invalid syntax (<stdin>, line 1)

and hit the return key, I get the error message *** SyntaxError: invalid syntax (<stdin>, line 1)

如何在PDB中执行多行语句?如果不可能,是否有办法解决仍然执行if子句或for循环的问题?

How can one execute multi-line statements within PDB? If not possible is there a way around this to still executing an if clause or a for loop?

推荐答案

您可以在pdb中执行此操作,以使用所有可用的本地变量启动临时的交互式Python会话:

You could do this while in pdb to launch a temporary interactive Python session with all the local variables available:

(pdb) !import code; code.interact(local=vars())
Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> 

完成后,使用Ctrl-D返回常规pdb提示符.

When you're done, use Ctrl-D to return to the regular pdb prompt.

只需不按Ctrl-C,这将终止整个pdb会话.

Just don't hit Ctrl-C, that will terminate the entire pdb session.

这篇关于如何在Python自己的调试器(PDB)中执行多行语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆