Skip to content

Instantly share code, notes, and snippets.

@giohappy
Last active March 7, 2024 15:47
Show Gist options
  • Save giohappy/8a30f14678aa7e446f9b694c632d7089 to your computer and use it in GitHub Desktop.
Save giohappy/8a30f14678aa7e446f9b694c632d7089 to your computer and use it in GitHub Desktop.
QGIS Python plugin debuggin with VS Code on Windows

A debugpy version inspired by this gist: https://gist.github.com/NicolaiMogensen/acfd8723720c4761aefef3cdfc2aa55a

Debugging QGIS 3.x python plugins on Windows 11 using VS Code

Setting up VS Code

NOTE: I'm using VS Code Python extension version 2022.20.2, which has PyLance as language server. The most recent versions of the extension use debugpy debugger.

Start VS Code

For simplicity I start VS Code directly from the plugin folder. We could symlink to avoid polluting the folder with out configurations...

settings.json

{
    "python.defaultInterpreterPath": "C:\\OSGeo4W\\apps\\Python39\\python.exe",
    "python.analysis.extraPaths": [
        "C:\\OSGeo4W\\apps\\qgis\\python",
        "C:\\Users\\<USERNAME>\\.vscode\\extensions\\ms-python.python-2022.20.2\\pythonFiles\\lib\\python"
    ],
    "terminal.integrated.env.windows": {
        "PYTHONPATH": "C:\\OSGeo4W\\apps\\qgis\\python\\qgis",
        "PATH": "C:\\OSGEO4~1\\apps\\qgis\\bin;C:\\OSGEO4~1\\apps\\Python37;C:\\OSGEO4~1\\apps\\Python37\\Scripts;C:\\OSGEO4~1\\apps\\qt5\\bin;C:\\OSGEO4~1\\apps\\Python27\\Scripts;C:\\OSGEO4~1\\bin;C:\\WINDOWS\\system32;C:\\WINDOWS;C:\\WINDOWS\\system32\\WBem;C:\\OSGEO4~1\\apps\\Python37\\lib\\site-packages\\pywin32_system32;C:\\OSGEO4~1\\apps\\Python37\\lib\\site-packages\\numpy\\.libs",
        "GDAL_DATA": "C:\\OSGEO4~1\\share\\gdal",
        "GDAL_DRIVER_PATH": "C:\\OSGEO4~1\\bin\\gdalplugins",
        "GDAL_FILENAME_IS_UTF8": "YES",
        "GEOTIFF_CSV": "C:\\OSGEO4~1\\share\\epsg_csv",
        "O4W_QT_BINARIES": "C:\\OSGEO4~1\\apps\\Qt5\\bin",
        "O4W_QT_DOC": "C:\\OSGEO4~1\\apps\\Qt5\\doc",
        "O4W_QT_HEADERS": "C:\\OSGEO4~1\\apps\\Qt5\\include",
        "O4W_QT_LIBRARIES": "C:\\OSGEO4~1\\apps\\Qt5\\lib",
        "O4W_QT_PLUGINS": "C:\\OSGEO4~1\\apps\\Qt5\\plugins",
        "O4W_QT_PREFIX": "C:\\OSGEO4~1\\apps\\Qt5",
        "O4W_QT_TRANSLATIONS": "C:\\OSGEO4~1\\apps\\Qt5\\translations",
        "QT_PLUGIN_PATH": "C:\\OSGEO4~1\\apps\\qgis\\qtplugins;C:\\OSGEO4~1\\apps\\qt5\\plugins",
        "QGIS_PREFIX_PATH": "C:\\OSGEO4~1\\apps\\qgis",
    }
}  

Replace USERNAME with your Windows user.

Launch.json

Remote debugger configuration for debugpy

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Remote Attach",
            "type": "python",
            "request": "attach",
            "port": 5678,
            "host": "localhost",
            "pathMappings": [
                {
                    "localRoot": "${workspaceFolder}",
                    "remoteRoot": "${workspaceFolder}"
                }
            ]
        }
    ]
}

Running the debugger inside the plugin

The DebugVS QGIS Plugin only supports the deprecated ptvsd debugger, so we cannnot take advantage of it. We have to import debugpy and run the debug adapter instance in our code.

I add the following snippet to the __init__.classFactory method:

import sys
sys.path.append('C:\\Users\\<USERNAME>\\.vscode\\extensions\\ms-python.python-2022.20.2\\pythonFiles\\lib\\python')

import debugpy
import shutil
debugpy.configure(python=shutil.which("python"))
try:
	debugpy.listen(("localhost", 5678))
except:
	debugpy.connect(("localhost", 5678))

This snippet

  • adds the debugpy module to the Python path
  • configures the DAG adapter
  • runs the adapter server
  • if it fails it tries to connect to an existing instance. This is a simple way to be able to repload the plugin (with the Plugin Reloader plugin), since debugpy doesn't offer an API to stop it. If we try to listen again on the same port we get a RuntimeError

Now you should be ready to debug!

Debugging

  • Run QGIS
  • In VS Code start debugging using the Python: Remote Attach configuration defined above.

Now you should be able to set breakpoints in VS Code.

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