Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
991 views
in Technique[技术] by (71.8m points)

debugging - How to execute multi-line statements within Python's own debugger (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's version of PDB, though for the matter I don't think it makes a difference; I use it for the colored output only).

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:

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)
>>> 

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...