I'm debugging a python script, and I want to watch a variable for a change (much like you can watch a memory adress in gdb). Is there a way to do this?
Enter p variable_name at the (Pdb) prompt to print its value.
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().
...much like you can watch a memory address in gdb...
For watching a variable when you are hitting a breakpoint, you can use the commands
command. E.g. printing some_variable
when hitting breakpoint #1 (canonical example from pdb
doc).
(Pdb) commands 1 (com) print(some_variable) (com) end (Pdb)
Additionally, you can use the condition
command to ensure the breakpoint is only hit whenever the variable takes a certain value.
eg:
(Pdb) condition 1 some_variable==some_value
You can use tracing / profiling functions to examine things step by step using sys.settrace
and checking out the opcodes being executed.
https://docs.python.org/3/library/sys.html#sys.settrace
Here is some code to get you started:
import sys import dis def tracefn(frame, event, arg): if event == 'call': print("## CALL", frame) frame.f_trace_opcodes = True elif event == 'opcode': opcode = frame.f_code.co_code[frame.f_lasti] opname = dis.opname[opcode] print("## OPCODE", opname) return tracefn watchme = 123 def foo(): global watchme watchme = 122 sys.settrace(tracefn) foo()
You will probably need to spy on all the STORE_*
opcodes. https://docs.python.org/3/library/dis.html
For Python 3:
you can use display functionality of pdb
Once you hit the breakpoint just type
ipdb> display expression
example:
ipdb> display instance display instance: <AppUser: dmitry4> ipdb> display instance.id display instance.id: 9 ipdb> display instance.university display instance.university: <University: @domain.com> ipdb> display Currently displaying: instance.university: <University: @domain.com> instance.id: 9 instance: <AppUser: dmitry4> ipdb>
as you can see, each time you type display - it will print all of your watches (expressions). You can use builtin function undisplay
to remove certain watch.
You can also use pp expression to prettyprint the expression (very useful)
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