Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I list the current line in python PDB?

Tags:

python

pdb

In the perl debugger, if you repeatedly list segments of code taking you away from the current line, you can return to the current line by entering the command . (dot).

I have not been able to find anything comparable using the python PDB module. If I list myself away from the current line and want to view it again, it seems I have to either remember the line number that was currently executing (unlikely for me) or execute a statement (often undesirable).

Am I missing something?

like image 433
zenzic Avatar asked Mar 02 '11 15:03

zenzic


People also ask

How do you go to the next line in pdb?

The difference between n (next) and s (step) is where pdb stops. Use n (next) to continue execution until the next line and stay within the current function, i.e. not stop in a foreign function if one is called.

How do I use pdb code 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().

What is pdb Set_trace ()?

pdb. set_trace (*, header=None) Enter the debugger at the calling stack frame. This is useful to hard-code a breakpoint at a given point in a program, even if the code is not otherwise being debugged (e.g. when an assertion fails). If given, header is printed to the console just before debugging begins.


2 Answers

Late but hopefully still helpful. Make the following alias:

alias ll u;;d;;l 

Then whenever you type ll, pdb will list from the current position. It works by going up the stack and then down the stack, which resets 'l' to show from the current position. (This won't work if you are at the top of the stack trace.)

Tip: Permanent alias

To make the alias permanent, add the line into your .pdbrc-file in the user home directory (~/.pdbrc). This works with both pdb and ipdb.

like image 109
Ghopper21 Avatar answered Sep 21 '22 12:09

Ghopper21


In Python 3.2 and higher, you can use list . to reset the list location.

Source: Python Bug tracker #4179

like image 38
CoupleWavyLines Avatar answered Sep 21 '22 12:09

CoupleWavyLines