My code is, for better or worse, rife with single letter variables (it's physics stuff, so those letters are meaningful), as well as NumPy's, which I'm often interacting with.
When using the Python debugger, occasionally I'll want to look at the value of, say, n
. However, when I hit n<enter>
, that's the PDB command for (n)ext
, which has a higher priority. print n
works around looking at it, but how can I set it?
Optionally, you can also tell pdb to break only when a certain condition is true. Use the command b (break) to set a breakpoint. You can specify a line number or a function name where execution is stopped. If filename: is not specified before the line number lineno , then the current source file is used.
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.
If you enter pdb interactive mode there is no way to return to ipdb or ipython. The proper way to exit is by using ctrl-d .
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.
Use an exclamation mark !
before a statement to have it run :
python -m pdb test.py > /home/user/test.py(1)<module>() -> print('foo') (Pdb) !n = 77 (Pdb) !n 77 (Pdb) n foo > /home/user/test.py(2)<module>() -> print('bar') (Pdb)
The docs say:
! statement
Execute the (one-line) statement in the context of the current stack frame. The exclamation point can be omitted unless the first word of the statement resembles a debugger command. [...]
You can use semicolons, so just put something else in front of it:
ipdb> print n 2 ipdb> n > 145 <some code here> 146 147 ipdb> 1; n=4 1 ipdb> print n 4
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With