Quick tips on using pdb to debug code of installed packages in Colab
When running code in Colab, there are occasion I need to debug code that’s not develop by me but from installed packages, and it’s impossible alter code inside.
When code is written by myself, it’s easy to add code:
from pdb import set_trace; set_trace()
So when code run to this line, it would trigger the debugging:
Note that when debugging, hitting “n” (next) does not lead us to next line in code (line 5), one would need to experiment with some “n” or “s” (step) get to the line:
In order to get inside the enumerate function for running the dataloader, it would need a lot more “n” or “s” or “up”, and sometimes just got passed the line of code and need to repeat the same process again.
The reason is there are a lot of Colab/Jupter notebook code that need to run until that line I want to debug.
The solution is actually trivial, because we have the list_dataloader object instance, we can actually tell the debugger to set break point to the instance function inside the data loader, which is list_dataloader.__iter__()
Hit “c” (continue) after setting breakpoint, I arrived at the __iter__() of the dataloader instance.
On the other hand, if we do not have an instance at current debug scope, and would like to add breakpoint to a class, we would do similar add breakpoint command.
Sometimes you might want to break at a line that’s in the middle of a function, you can set a breakpoint at a particular line.
But how can we know which line? Colab give a very easy UX, click the “View source” link
The code would open on the right pane, the explore the file and locate the line you need to set the breakpoint.
Wish this help someone.