Skip to content

Instantly share code, notes, and snippets.

@giohappy
Last active November 6, 2024 17:47
Show Gist options
  • Save giohappy/637c57fdcbad1a97e0d3a6e314904cef to your computer and use it in GitHub Desktop.
Save giohappy/637c57fdcbad1a97e0d3a6e314904cef to your computer and use it in GitHub Desktop.

A generic version built on top of https://gist.github.com/giohappy/8a30f14678aa7e446f9b694c632d7089

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

Setting up VS Code

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

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\\Python312\\python.exe",
    "python.analysis.extraPaths": [
        "C:\\OSGeo4W\\apps\\qgis\\python"
    ],
    "terminal.integrated.env.windows": {
        "PYTHONPATH": "C:\\OSGeo4W\\apps\\qgis\\python\\qgis",
        "PATH": "C:\\OSGEO4~1\\apps\\qgis\\bin;C:\\OSGEO4~1\\apps\\Python312;C:\\OSGEO4~1\\apps\\Python312\\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\\Python312\\lib\\site-packages\\pywin32_system32;C:\\OSGEO4~1\\apps\\Python312\\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",
    }
}  

Launch.json

Remote debugger configuration for debugpy

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

Running the debugger inside the plugin

I have modified the DebuVS QGIS Plugin, and I have bundled the VS vendored debugpy module since I haven't been able to install and load it from the QGIS plugin environment (_builtin_import error). The modified plugin (for Windows) can be taken from https://github.com/giohappy/debug_vs_plugin/tree/windows.

The DebuVS QGIS Plugin needs debugpy. It can be installed by opening the OSGeo4W Shell and running pip3 install debugy. BTW the original plugin throws strange errors if debugpy is not explicitely configured for python3 (at least on Windows). I've added this line to the plugin and now it loads correctly.

Then:

  • place the plugin under the QGIS Plugins folder (C:\Users\giohappy\AppData\Roaming\QGIS\QGIS3\profiles\default\python\plugins)
  • Run QGIS
  • 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.

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