Today we are going to talk about debugging.
We as python developers have several choices when it comes to debugging. Starting from the old and standard pdb to newer and fancier ipdb and web-pdb. When debugging Python code, often we use a standard pattern such as -
import <debugger_module>
<lines_of_code>
<debugger_module>.set_trace()
<more_lines_of_code>
And the porgram halts at the line `<debugger_module>.set_trace()`. It presents you with an interactive debugger where you can examine the values of the variables, run arbitary code, and walk through the code step by step.
So far so good. But I am going to ask you two questions whose answers are not so clear given the above pattern.
Assuming that you have many set_trace, in many different parts of the code (based on code branching and other factors) how do you easily switch debuggers? Such as, if you are using pdb now and want to use ipdb tomorrow how do you go about that?
Assuming that you have debug statements in your code, how can you confidently deploy that in production?
Answer to these questions is in the newly introduced `breakpoint` builtin function. There is only one thing to keep in mind, you need Python -
3.7+
So here is how it works - You alter the code to the following pattern
<lines_of_code>
breakpoint()
<more_lines_of_code>
In this example, the breakpoint serves just as a convenience function, calling sys.breakpoint under the hood which in turn will invoke the default pdb debugger.
But
The magic is that you can control the behavior at rum time. For an example, imagine that you want to use ipdb instead of pdb. You keep your code as it is and then just invoke the script like the following.
PYTHONBREAKPOINT=ipdb.set_trace python my_script.py
That’s it. The environment variable PYTHONBREAKPOINT controls the behavior of the sys.breakpoint function and thus by setting its value to a different debugger means it will invoke the same at run time.
Finally, you can also run your script, which contains breakpoint, without starting the debugger. To do so simply set the value of PYTHONBREAKPOINT as zero. Like so -
PYTHONBREAKPOINT=0 my_script.py
So you can deploy the same codebase without thinking about removing the debugging related code. Isn’t that cool? :)
If you like Python’s byte, please do subscribe, like, and share.
From around the web
FAIR releases MBRL-Lib a PyTorch-based toolbox for research on model-based reinforcement learning - https://github.com/facebookresearch/mbrl-lib
Logica: language of Big Data - https://github.com/EvgSkv/logica
The Neural Process Family - https://yanndubs.github.io/Neural-Process-Family/text/Intro.html
TorchDrift is a data and concept drift library for PyTorch - https://github.com/torchdrift/torchdrift/
America’s most promising artificial intelligence companies of 2021 - https://www.forbes.com/sites/alanohnsman/2021/04/26/ai-50-americas-most-promising-artificial-intelligence-companies/?sh=162025a577cf