In QGIS install the plugin debugvs
.
The debugvs
plugin needs the python module ptvsd
to function. This module is not installed by default.
In principle you just pip install ptvsd
in the python interpreter used by QGIS.
I am using the QGIS OSX installer from Lutra Consulting. This installer works really great, but installing additional python modules is not very easy.
What I did was this:
- Download the
ptvsd
wheel from pypi. I tried with the newest version (4.2.4), but that didnt work for me. I ended up using ptvsd-4.1.4.zip. - Unzip the wheel (if it has the extension
.whl
then rename it to.zip
) - Inside the zip there are two directories. Copy the directory
ptvsd
to theResources/python
of your QGIS installation. In my case this was/Applications/QGIS3.6.app/Contents/Resources/python/
.
Restart QGIS.
The default remote debugger configuration in VS Code looks like this
{
"name": "Python: Remote Attach",
"type": "python",
"request": "attach",
"port": 5678,
"host": "localhost",
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "."
}
]
},
I had to change the pathMappings
to get it to work:
{
"name": "Python: Remote Attach",
"type": "python",
"request": "attach",
"port": 5678,
"host": "localhost",
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "${workspaceFolder}"
}
]
},
- In QGIS click
Plugins -> Enable Debug for Visual Studio -> Enable Debug for Visual Studio
- You should now see a message in the QGIS message bar saying something like
DebugVS : Run the Debug in Visual Studio(Python:Attach)
- In VS Code start debugging using the
Python: Remote Attach
configuration defined above.
Now you should be able to set breakpoints in VS Code.
so update for everyone again struggling (as myself) to connect to debug plugins in March 2025 to the ... present version of QGIS 3.40.something.
as you can see below this is not conda, no venv, no fancy stuff, just straight system config, as qgis comes with its own python anyway. my install is the OSGeo4W install, though pretty sure this should work with other stripped-down versions.
contrary to what it was in 2019, now vscode-hosted debugging is possible solely with
debugpy
and this is the prolog which goes in init.py for my pluginsnotes:
debugpy.wait_for_client()
is grayed out as it causes basically causes QGIS to reload. At one point it was like a fork bomb actually, very vad. I thought the python path was the issue initially, perhaps is not. Others also complain, I could find some cries for help on various forums. So if you want to wait for your debugger, perhaps another logic should be implemented, thing is it WORKS as it is now, given the plugin compiles and imports modules properly, and then you can async connect to the debugpy which is alredy working in some separate light interpreter thread I guess (not sure the mechanics though).debugpy
from the python console in QGIS, or via the OSGeo4W, I trust you know how to do it .dubgvs
is not needed anymore as pluginthe struggle, hence the length of this
setup_debugging
, was to map the dev dir to the qgis dir. not sure how you guys develop, but I have the code in separate dir from the plugins dir, and neither I use the additional plugin dirs feature. Claude created for me a deploy script such that it watches over files and copies those modified to the plugin dir (on windows one also has to explicitly allow this, as c:\Program Files\ is protected... perhaps my setup is a bit unpopular one, but works OK for me).I spent like a day remembering how exactly this mapping thing should normally be, and eventually ended with this
launch.json
Here,
localRoot
typically equals to your current dir, the project dir. TheremoteRoot
changes depending on where you deploy (or if you have had QGIS load plugin directly from the dev dir). I decided to not try to set it automatically, though is perhaps possible with some prerun script to get the QGIS location for the user (and unless, again he has 3 different versions like myself). Most tutorials have you setremoteRoot
to the same dir aslocalRoot
which should (havent tested it) work when you develop and load/run the plugin from one and the same dir.Also make sure that you've set the python compiler for pylance to work properly and the
extraPaths
. here's a Qt5 config that worksfor everyone reading this far, perhaps you should note that
"c:/Work/fmigis/apify"
in the example is the project homedir, so update this accordingly. this is basically the git project root. also thesepython.analysis
directives were absolutely necessarily to get the linter read the correct dirs. also, the selected language server ispylance
and the analysis is done by it, hence your linter may behave differently.Note - it is very likely that this will not work with newer PyQt6 versions, still experimental at time of writing. QGIS devs introduced a compatibility layer, but I couldn't find a way to have all the linters understand its magic, thus my imports were littered with warnings which impact the dev process, and my progress respectively.
Good luck, and I may actually release the plugin I'm writing atm, I just needed to share findings once the vscode connected, as this obstacle really drove me crazy.
...