Skip to content

Instantly share code, notes, and snippets.

@giohappy
Last active November 15, 2024 11:58
Show Gist options
  • Save giohappy/61e6ce6d5e80c482ead875f6ac47a5d6 to your computer and use it in GitHub Desktop.
Save giohappy/61e6ce6d5e80c482ead875f6ac47a5d6 to your computer and use it in GitHub Desktop.

Debugging QGIS 3.x python plugins on Ubuntu 22.04 using VS Code

My setup uses Ubuntu 22.04 running on Windows WSL2, which suppots executing GUI applications.

Setting up VS Code

NOTE: I'm using VS Code Python Debugger extension version v2024.12.0 (ms-python.debugpy), which uses debugpy.

QGIS

In my setup I'm running QGIS built from source, following the official instructions. My build is under /home/giohappy/dev/qgis/QGIS/build.

Configure VS Code

I start VS Code from the plugin folder. In my case I'm testing QGISGeoNodePlugin that I have cloned from Github.

settings.json

{
    "terminal.integrated.env.linux": {
        "PYTHONPATH": "/home/giohappy/dev/qgis/QGIS/build/output/python",
    },
    "python.analysis.include": [
        "/home/giohappy/dev/qgis/QGIS/build/output/python"
    ],
    "python.analysis.extraPaths": [
        "/home/giohappy/dev/qgis/QGIS/build/output/python"
    ]
}  

NOTE: PyQT5 is installed on the machine, since they have been installed from apt to build QGIS form source, so I don't need to add it to the settings.

Launch.json

Remote debugger configuration for debugpy

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Remote Attach",
            "type": "debugpy",
            "request": "attach",
            "connect": {
                "port": 5678,
            }
        }
    ]
}

Build the plugin

From the plugin folder (e.g. /home/giohappy/dev/qgis/QGISGeoNodePlugin) run:

  • `python -m venv venv && ./venv/bin/activate && pip install poetry
  • pip install . to install the build deps
  • python pluginadmin.py build

The plugin will be created under the build folder.

Then we symlink the plugin to the QGIS plugins folder:

  • ln -s /home/giohappy/dev/qgis/QGISGeoNodePlugin/build/qgis_geonode/ /home/giohappy/.local/share/QGIS/QGIS3/profiles/default/python/plugins/qgis_geonode

Running the debugger inside the plugin

The DebuVS QGIS Plugin needs debugpy. I have installed it globallty with pip3 install debugy.

Then: )

  • Run QGIS
  • Enable the plugin from the Plugins Installer, if not yet enabled
  • Under the plugins menu click "Enable Debuf for Visual Studio"
  • If all goes fine the message bar should print "INFO DebugVS : Remote Debug for Visual Studio is running("request": "enable_attach", "Port": 5678, "host": "localhost")"

Now you should be ready to debug!

Debugging

In VS Code

  • start debugging using the Python: Remote Attach configuration defined above.
  • Place the breakpoints inside the code inside the build folder.

Now you should be able to step debug in VS Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment