A debugpy
version inspired by this gist: https://gist.github.com/NicolaiMogensen/acfd8723720c4761aefef3cdfc2aa55a
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.
For simplicity I start VS Code directly from the plugin folder. We could symlink to avoid polluting the folder with out configurations...
{
"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.
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}"
}
]
}
]
}
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 thePlugin Reloader
plugin), sincedebugpy
doesn't offer an API to stop it. If we try to listen again on the same port we get aRuntimeError
Now you should be ready to debug!
- 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.