Tuesday, January 7, 2020

Python debugging

Some tips for debugging in python :

1) Always use an editor to modify code (VS code in my case). Python is very strict about indentation, and if you mess it up, it will complain and complain!

2) Two golden lines to help you debug :

import pdb
pdb.set_trace()

Add this into each file, at whatever position you want it to stop in the pdb interactive debugger.

3) Commands for debugging
l - prints a snippet of the lines of code before and after the current line.
n - to step over next function
s - to step into next function
c - to continue execution
enter key - to execute previous command
pp - to pretty print the object
p - to print
where - to print the stack trace during execution (so very useful)

Happy debugging!

No comments:

Post a Comment