Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute multi-line statements within Python's own debugger (PDB)

People also ask

Can the debugger execute multiple lines of code simultaneously?

For example, the correlator might simultaneously execute multiple statements over multiple lines even if you are using debugger commands to step through the program line by line. To debug applications on a running correlator, run the following command: engine_debug [ [ global-options ] [command [options]] ... ]

How do I use pdb debugger in Python?

Starting Python Debugger To start debugging within the program just insert import pdb, pdb. set_trace() commands. Run your script normally and execution will stop where we have introduced a breakpoint. So basically we are hard coding a breakpoint on a line below where we call set_trace().

Is pdb a debugging module in Python?

The module pdb defines an interactive source code debugger for Python programs. It supports setting (conditional) breakpoints and single stepping at the source line level, inspection of stack frames, source code listing, and evaluation of arbitrary Python code in the context of any stack frame.

What does import pdb pdb Set_trace () do?

It's import pdb; pdb. set_trace() . When execution reaches this point in the program, the program stops and you're dropped into the pdb debugger. Effectively, this is the same as inserting a breakpoint on the line below where we call set_trace() .


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.


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.


My recommendation is to use IPython embedding.

ipdb> from IPython import embed; embed()

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.


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.


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.