Mike
Mike

Reputation: 2501

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?

Upvotes: 250

Views: 91032

Answers (6)

kirin
kirin

Reputation: 1960

My recommendation is to use IPython embedding.

ipdb> from IPython import embed; embed()

Upvotes: 62

vaer-k
vaer-k

Reputation: 11773

In python3 ipdb (and pdb) have a command called interact. It can be used to:

Start an interactive interpreter (using the code module) whose global namespace contains all the (global and local) names found in the current scope.

To use it, simply enter interact at the pdb prompt. Among other things, it's useful for applying code spanning multiple lines, and also for avoiding accidental triggering of other pdb commands.

Upvotes: 174

Marius Gedminas
Marius Gedminas

Reputation: 11367

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.

Upvotes: 335

ThomasH
ThomasH

Reputation: 23536

There is the special case if you want a couple of commands be executed when hitting a break point. Then there is the debugger command commands. It allows you to enter multiple lines of commands and then end the whole sequence with the end key word. More with (pdb) help commands.

Upvotes: 7

Garrett Hyde
Garrett Hyde

Reputation: 5637

Inside the Python (2.7.1) interpreter or debugger (import pdb), you can execute a multi-line statement with the following syntax.

for i in range(5): print("Hello"); print("World"); print(i)

Note: When I'm inside the interpreter, I have to hit return twice before the code will execute. Inside the debugger, however, I only have to hit return once.

Upvotes: 40

Zach Kelling
Zach Kelling

Reputation: 53879

I don't know if you can do this, that'd be a great feature for ipdb though. You can use list comprehensions of course, and execute simple multi-line expressions like:

if y == 3: print y; print y; print y;

You could also write some functions beforehand to do whatever it is you need done that would normally take multiple lines.

Upvotes: 2

Related Questions