Skip to content

Instantly share code, notes, and snippets.

@AsgerPetersen
Last active April 27, 2024 13:27
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save AsgerPetersen/9ea79ae4139f4977c31dd6ede2297f90 to your computer and use it in GitHub Desktop.
Save AsgerPetersen/9ea79ae4139f4977c31dd6ede2297f90 to your computer and use it in GitHub Desktop.
Debugging QGIS 3.x python plugins on OSX using VS Code

Debugging QGIS 3.x python plugins on OSX using VS Code

Plugin

In QGIS install the plugin debugvs.

Python dependencies

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:

  1. 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.
  2. Unzip the wheel (if it has the extension .whl then rename it to .zip)
  3. Inside the zip there are two directories. Copy the directory ptvsd to the Resources/python of your QGIS installation. In my case this was /Applications/QGIS3.6.app/Contents/Resources/python/.

Restart QGIS.

Setting up VSCode

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}"
        }
    ]
},

Debugging

  • 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.

@pktrigg
Copy link

pktrigg commented Jan 14, 2024

Qgis 3.32 (Lima) installed from Osgeo4w. I followed instructions (install ptvsd then debugpy and installed plugin debugvs). When I launch "Plugins->"Enable debug for Visual Studio" actually it:

  • launches another qgis desktop giving this 8 errors:
    err 1. Invalid data source: C:\Users.......\stringofcodes is not a valid or recognize data source
    err 2. Invalid Data Source: C:\Users....\Documents--server-access-token is not valid or recognize data source
    err 3. the same with C:\Users....\Documents\5678
    err 4. the same with --port
    err 5. the same with localhost
    ...

Actually I don't know how to solve it. I tried also a fresh installation etc. always the same result.

Someone can suggest how to solve it or a workaround?

Hi
I get the same 8 errors when I start the debug plugin. It also opens a new instance of qgis. I have been fighting this all day without luck.
am using qgis 3.34.1 on windows 10
as far as I know we dont need to install pvtsd with the current version of the plugin. either way we get the same 8 errors here.

2024-01-14T18:44:30 CRITICAL Invalid Data Source : C:\Users\Operator\AppData\Roaming\Python\Python39\site-packages\debugpy\adapter is not a valid or recognized data source.
2024-01-14T18:44:30 CRITICAL Invalid Data Source : C:\Users\Operator\Documents--for-server is not a valid or recognized data source.
2024-01-14T18:44:30 CRITICAL Invalid Data Source : C:\Users\Operator\Documents\49899 is not a valid or recognized data source.
2024-01-14T18:44:30 CRITICAL Invalid Data Source : C:\Users\Operator\Documents--host is not a valid or recognized data source.
2024-01-14T18:44:30 CRITICAL Invalid Data Source : C:\Users\Operator\Documents\localhost is not a valid or recognized data source.
2024-01-14T18:44:30 CRITICAL Invalid Data Source : C:\Users\Operator\Documents--port is not a valid or recognized data source.
2024-01-14T18:44:30 CRITICAL Invalid Data Source : C:\Users\Operator\Documents\5678 is not a valid or recognized data source.
2024-01-14T18:44:30 CRITICAL Invalid Data Source : C:\Users\Operator\Documents--server-access-token is not a valid or recognized data source.
2024-01-14T18:44:30 CRITICAL Invalid Data Source : C:\Users\Operator\Documents\b8f00babbe3d5c52c125545fafdad7f044eaa73b1ecac8671d6c01aa3d6bd75c is not a valid or recognized data source.
2024-01-14T18:48:47 WARNING Python error : An error has occurred while executing Python code: See message log (Python Error) for more details.

@mariochermes
Copy link

mariochermes commented Feb 4, 2024

Qgis 3.32 (Lima) installed from Osgeo4w. I followed instructions (install ptvsd then debugpy and installed plugin debugvs). When I launch "Plugins->"Enable debug for Visual Studio" actually it:

  • launches another qgis desktop giving this 8 errors:
    err 1. Invalid data source: C:\Users.......\stringofcodes is not a valid or recognize data source
    err 2. Invalid Data Source: C:\Users....\Documents--server-access-token is not valid or recognize data source
    err 3. the same with C:\Users....\Documents\5678
    err 4. the same with --port
    err 5. the same with localhost
    ...

Actually I don't know how to solve it. I tried also a fresh installation etc. always the same result.
Someone can suggest how to solve it or a workaround?

Hi I get the same 8 errors when I start the debug plugin. It also opens a new instance of qgis. I have been fighting this all day without luck. am using qgis 3.34.1 on windows 10 as far as I know we dont need to install pvtsd with the current version of the plugin. either way we get the same 8 errors here.

2024-01-14T18:44:30 CRITICAL Invalid Data Source : C:\Users\Operator\AppData\Roaming\Python\Python39\site-packages\debugpy\adapter is not a valid or recognized data source. 2024-01-14T18:44:30 CRITICAL Invalid Data Source : C:\Users\Operator\Documents--for-server is not a valid or recognized data source. 2024-01-14T18:44:30 CRITICAL Invalid Data Source : C:\Users\Operator\Documents\49899 is not a valid or recognized data source. 2024-01-14T18:44:30 CRITICAL Invalid Data Source : C:\Users\Operator\Documents--host is not a valid or recognized data source. 2024-01-14T18:44:30 CRITICAL Invalid Data Source : C:\Users\Operator\Documents\localhost is not a valid or recognized data source. 2024-01-14T18:44:30 CRITICAL Invalid Data Source : C:\Users\Operator\Documents--port is not a valid or recognized data source. 2024-01-14T18:44:30 CRITICAL Invalid Data Source : C:\Users\Operator\Documents\5678 is not a valid or recognized data source. 2024-01-14T18:44:30 CRITICAL Invalid Data Source : C:\Users\Operator\Documents--server-access-token is not a valid or recognized data source. 2024-01-14T18:44:30 CRITICAL Invalid Data Source : C:\Users\Operator\Documents\b8f00babbe3d5c52c125545fafdad7f044eaa73b1ecac8671d6c01aa3d6bd75c is not a valid or recognized data source. 2024-01-14T18:48:47 WARNING Python error : An error has occurred while executing Python code: See message log (Python Error) for more details.

@pktrigg I don't know if you were able to solve this. But here is how I did it:

  1. Find your QGIS plugins folder, it generally is somewhere like:
    C:\Users\user\AppData\Roaming\QGIS\QGIS3\profiles\default\python\plugins
  2. Look for debug_vs and open it's init.py file.
  3. Where debugpy is imported, add the line: self.debugpy.configure(python = 'python3')
    The result should look something like this:
        import debugpy

        self.debugpy = debugpy
        self.debugpy.configure(python = 'python3')
  1. Reload the plugin (or QGIS altogether), it should work now.

@pktrigg
Copy link

pktrigg commented Feb 4, 2024 via email

@rhinejoel
Copy link

rhinejoel commented Feb 13, 2024

@pktrigg I don't know if you were able to solve this. But here is how I did it:

  1. Find your QGIS plugins folder, it generally is somewhere like:
    C:\Users\user\AppData\Roaming\QGIS\QGIS3\profiles\default\python\plugins
  2. Look for debug_vs and open it's init.py file.
  3. Where debugpy is imported, add the line: self.debugpy.configure(python = 'python3')
    The result should look something like this:
        import debugpy

        self.debugpy = debugpy
        self.debugpy.configure(python = 'python3')
  1. Reload the plugin (or QGIS altogether), it should work now.

@AsgerPetersen
@mariochermes I did that and resolved the 8 errors as well as the 2nd instance of QGIS. However, this persists.

RuntimeError: timed out waiting for adapter to connect

macOS 14.1.2
QGIS 3.34

Screenshot 2024-02-13 at 4 55 09 PM

@rhinejoel
Copy link

Hi, from another post I realized that I needed to specify the python interpreter path in debugpy.configure() thus following this thread,

debugpy.configure(python=r"path_to_qgis_python_")
debugpy.listen(...)

I am now able to launch the server and connect via VS Code. Thank you!

@sanak
Copy link

sanak commented Apr 27, 2024

@rhinejoel
Thanks for the useful information!
Above path_to_qgis_python_ part was important in my case (using official download version (qgis-mac-ltr.dmg)), because specifying only python3 uses another system python (/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/Resources/Python.app/Contents/Python).
The following works on my environment.

@@ -48,6 +48,8 @@
             import debugpy
 
             self.debugpy = debugpy
+            self.debugpy.configure(python=r"/Applications/QGIS.app/Contents/MacOS/bin/python3")
+            self.debugpy.listen(...)
         except:
             pass
         self.port = 5678

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