The toughest and time-consuming problem for any developer in the world is to debug the code when something isn't going right. If you are doing it the wrong way by printing each statement out then you should start using one of the tools that we are going to see today.
PySnooper
This is one of the simple debugging tools where you will be able to log what is happening inside any code snippet that you write. It is also called poor man's debugging tool. It is highly useful in cases where you are returning or printing some value, but the printed one is not as you expected it to be.
As you can see in the above image that PySnooper basically logs all the lines that were executed. It gives a simple idea of what is actually happening in the function.
pdb++
The module ideally abbreviates to Python debugger, making it the ideal tool for developers to debug their code. pdb is a command based debugger, i.e. you can either execute the function to function or try finer granularity with line by line also. pdb is executed using certain commands, some of them are as follows
- 's' for skip
- 'c' for continue
- 'n' for next line
For more information on the command usage please visit the pdb++ homepage
Pycallgraph
This tool is an awesome one for developers who want to visualize how the code got executed. Below is a snippet of code which was executed in Google colab.
An image will be generated with each part denoting what has happened in each function defined
Show
This library is a very simple python wrapper that helps in knowing how each variable is defined by simply printing the variable along with its value. We can also add customization for colors
There are other advanced ways by which you can optimize and debug your code and some incredible tools are as follows:
- scalene: optimizes the code
- tbgrep: extracts the trackbacks
- vardbg: visualization of the bugs in your code
Thank you for reading the article and please give your valuable feedback on it. Hope you have a good day