Software development often consists of a brief period of pure excitementwhile writing the bulk of the source code, followed by a long and dreadedperiod of finding bugs and fixing them. This scheme is no different whenwriting plugins for QGIS.
Because we also write QGIS plugins ourselves,we were thinking of how to ease the pain of getting rid of bugs -- andso we ended up creating the First Aid plugin,now available in the official QGIS plugin repository.
It is meant to be a Swiss army knife for QGIS plugin developers, a toolthat allows easy inspection of any Python code running within QGIS.This is important because it can potentially save developers a lotof their valuable time.
How many times did you end up adding "print" statementsinto the code to find out what was going wrong in your code? With First Aidplugin this should be no longer necessary.
So let me explain what does it do. First of all, it comes with an improvedPython error handler. What this means is that whenever an error occurs in codeof a plugin, a window with all the details comes up.
Previously in QGIS you could only find out what was the exception's type, message and stack trace.First Aid plugin adds source code view, variables view and even an embeddedPython console where you can further inspect the state of the plugin at thetime of the error. Here is how it looks like in action:
Plugins however do not always come up with errors that can be caught and handledby QGIS. More often plugins simply do not behave as one would expect them to.Here is when people usually resort to using debuggers. There are IDEs likePyDev or PyCharm - or even standalone tools like Winpdb - that allow developersto do remote debugging. Basically they can connect to the Python environment withinQGIS and debug the code there. Personally, I have never been a big fan of thisapproach and found remote debugging cumbersome to set up and use. And trying to debugsomething on a client's computer is even a greater challenge.
First Aid fortunately integrates a debugger into QGIS environment. This allowsdevelopers to simply open the debugger window, load some Python files, setbreakpoints and everything is ready. Once QGIS reaches a line with a breakpoint,the debugger window will be activated and it is possible to step throughthe code and inspect the variables to understand what is going on in the code.
The great thing is that once the execution of Python code is stopped, it ispossible to step into code, step over, step out or run to cursor, just like in any other debugger.It is also possible to run custom scripts from within debugger window -- theywill be also run in debug mode.
Debugging is active only while the debugger window is still open. While debugging,there is some extra overhead when running any Python code (even for code thatyou do not intend to debug), so it is better to close the debugger when not needed.
The plugin has already helped us various times to quickly identify problemsin plugins. Having said that, please note that the plugin is still quiteyoung and may not work perfectly in all cases. We would be happy to hearyour feedback. Any issues orpull requests on GitHubwould be greatly appreciated!
Let's make the QGIS work for you
Lutra Consulting is a QGIS-focused expert provider of geospatial software development, consulting, training, and support services.