Debugging QGIS Python scripts
In this recipe, we will configure Eclipse to debug QGIS Python scripts.
How to do it…
Both QGIS and Eclipse must be configured for debugging so that the two pieces of software can communicate. Eclipse attaches itself to QGIS in order to give you insights into the Python scripts running in QGIS. This approach allows you to run scripts in a controlled way that can pause execution while you monitor the program to catch bugs as they occur.
Configuring QGIS
The following steps will add two plugins to QGIS, which allows Eclipse to communicate with QGIS. One plugin, Plugin Reloader, allows you to reload a QGIS plugin into memory without restarting QGIS for faster testing. The second plugin, Remote Debug, connects QGIS to Eclipse.
Remote Debug is an experimental plugin, so you must ensure that experimental plugins are visible to the QGIS plugin manager in the list of available plugins.
- Start QGIS.
- Under the Plugins menu, select ManageandInstallPlugins…
- In the left pane of the Plugins dialog, select the Settings tab.
- Scroll down in the Settings window and ensure that the Show also experimental plugins checkbox is checked, as shown in the following screesnhot:
- Click on the OK button.
- Select the tab labeled All in the pane on the left-hand side of the Plugins window.
- In the Search dialog at the top of the window, search for Plugin Reloader.
- Select Plugin Reloader from the search results and then click on the Install Plugin button.
- Next, search for the Remote Debug plugin and install it as well.
- Finally, install the HelloWorld plugin as well.
Configuring Eclipse
Now that QGIS is configured for debugging in Eclipse, we will configure Eclipse to complete the debugging communication loop, as shown in the following steps:
- Start Eclipse.
- In the File menu, select New and then click on Project.
- Select General and then click on Project from the NewProject dialog.
- Click on the Next> button.
- Give the project the name HelloWorldPlugin.
- Click on the Finish button.
- Select the new HelloWorldPlugin project in project explorer and select New; then, click on Folder from the File menu.
- In the New Folder dialog, click on the Advanced>> button.
- Choose the Link to alternate location (Linked Folder) radio button.
- Click on the Browse button and browse to the location of the
HelloWorldPlugin
folder, as shown in the following screenshot:Tip
You can find the location of the HelloWorld plugin from within the QGIS plugin manager.
- Click on the Finish button.
Testing the debugger
The previous parts of this recipe configured Eclipse and QGIS to work together in order to debug QGIS plugins. In this section, we will test the configuration using the simplest possible plugin, HelloWorld, to run Eclipse using the Debug Perspective. We will set up a break point in the plugin to pause the execution and then monitor plugin execution from within Eclipse, as follows:
- Under the
HelloWorld
folder, open the fileHelloWorld.py
. - From the Eclipse Window menu, select OpenPerspective and then click on Other…
- From the OpenPerspective dialog, select Debug.
- Click on the OK button.
- Scroll to the first line of the
hello_world()
function and double-click on the left-hand side of the line number to set a break point, which is displayed as a green-icon: - From the Pydev menu, select Start Debug Server.
- Verify that the server is running by looking for a message in the Debug console at the bottom of the window, similar to the following:
Debug Server at port: 5678
- Switch over to QGIS.
- From the QGIS Plugins menu, select RemoteDebug and then select the RemoteDebug command.
- Verify that the QGIS status bar in the lower-left corner of the window displays the following message:
Python Debugging Active
- Now, select HelloWorld from the QGIS Plugins menu and then select HelloWorld.
- Switch back to Eclipse.
- Verify that the
hello_world()
function is highlighted at the break point. - From the Run menu, select Resume.
- Switch back to QGIS.
- Verify that the HelloWorld dialog box has appeared.
How it works…
The RemoteDebug plugin acts as a client to the PyDev debug server in order to send the Python script's execution status from QGIS to Eclipse. While it has been around for several versions of QGIS now, it is still considered experimental.
The PluginReloader plugin can reset plugins that maintain state as they run. The HelloWorld plugin is so simple that reloading is not needed to test it repeatedly. However, as you debug more complex plugins, you will need to run it in order to reset it before each test. This method is far more efficient and easier to use than closing QGIS, editing the plugin code, and then restarting.
Note
You can find out more about debugging QGIS, including using other IDEs, at http://docs.qgis.org/2.6/en/docs/pyqgis_developer_cookbook/ide_debugging.html.