Usually when you develop an application it will not run on the same computer where you are developing. This can caused unexpected behaviors when you deploy it,usually cause by permissions, software/package versions,configuration,ect.. or just that we write an error on our code that we didn’t notice. In those cases, it’s difficult to know what is failing but we can use remote debug to check the application state when it fails.
Tip:
If you are trying to debug a python package and the debugger doesn’t stop, try to install it with the -e flag
I use two tools in these cases:
Visual studio code remote ssh
To remote debug (and develop) with Visual Studio code you mus to install Remote ssh extenison
Once you install it, you can add a host that looks like this:
And finally open a remote project:
After this, you can use Visual Studio code like on your own machine
PUDB
Pudb is a package to debug python applications via CLI. If you are used to and feel comfortable using CLI tools this can be a good tool to debug your python application.
To debug it with pudb you need to install the package with pip:
pip install pudb
On your code , add the following code where you want to stop:
import pudb;pu.db
Once the execution reaches the previous line, you will get a window that looks like this:
You can use the following shortcuts to interact with the pudb:
- Ctrl+x: Go to command line or return to the code
- n: Step to the next instruction
- s: Step into
- c: Continue
- b: Toggle breakpoint