Skip to content

Instantly share code, notes, and snippets.

@veuncent
Last active August 9, 2021 09:23
Show Gist options
  • Select an option

  • Save veuncent/c4589af14af42941ac0b0310e8240d6c to your computer and use it in GitHub Desktop.

Select an option

Save veuncent/c4589af14af42941ac0b0310e8240d6c to your computer and use it in GitHub Desktop.

Revisions

  1. veuncent revised this gist Sep 9, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion RemoteDebuggingQgisVsCode.md
    Original file line number Diff line number Diff line change
    @@ -58,7 +58,7 @@ def enable_remote_debugging():
    ## Debugging tasks / multithreading
    In order to line-by-line debug code started in another thread, e.g. when running a QTask, add `ptvsd.debug_this_thread()` after the thread split. E.g. in the `run()` function of your task (here from a function):
    ```
    def run(task, file_path):
    def run(task):
    import ptvsd
    ptvsd.debug_this_thread()
    ```
  2. veuncent revised this gist Oct 23, 2019. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions RemoteDebuggingQgisVsCode.md
    Original file line number Diff line number Diff line change
    @@ -59,6 +59,7 @@ def enable_remote_debugging():
    In order to line-by-line debug code started in another thread, e.g. when running a QTask, add `ptvsd.debug_this_thread()` after the thread split. E.g. in the `run()` function of your task (here from a function):
    ```
    def run(task, file_path):
    import ptvsd
    ptvsd.debug_this_thread()
    ```

  3. veuncent revised this gist Oct 23, 2019. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions RemoteDebuggingQgisVsCode.md
    Original file line number Diff line number Diff line change
    @@ -42,6 +42,7 @@ def enable_remote_debugging():
    except Exception as e:
    exc_type, exc_value, exc_traceback = sys.exc_info()
    format_exception = traceback.format_exception(exc_type, exc_value, exc_traceback)
    QgsMessageLog.logMessage(str(e), MESSAGE_CATEGORY, Qgis.Critical)
    QgsMessageLog.logMessage(repr(format_exception[0]), MESSAGE_CATEGORY, Qgis.Critical)
    QgsMessageLog.logMessage(repr(format_exception[1]), MESSAGE_CATEGORY, Qgis.Critical)
    QgsMessageLog.logMessage(repr(format_exception[2]), MESSAGE_CATEGORY, Qgis.Critical)
  4. veuncent revised this gist Oct 11, 2019. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions RemoteDebuggingQgisVsCode.md
    Original file line number Diff line number Diff line change
    @@ -54,6 +54,12 @@ def enable_remote_debugging():

    4. In VS Code start debugging using the Python: Remote Attach configuration defined above

    ## Debugging tasks / multithreading
    In order to line-by-line debug code started in another thread, e.g. when running a QTask, add `ptvsd.debug_this_thread()` after the thread split. E.g. in the `run()` function of your task (here from a function):
    ```
    def run(task, file_path):
    ptvsd.debug_this_thread()
    ```

    ## Acknowledgement
    This gist is based on one written by [AsgerPeterson](https://gist.github.com/AsgerPetersen/9ea79ae4139f4977c31dd6ede2297f90).
  5. veuncent revised this gist Oct 11, 2019. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion RemoteDebuggingQgisVsCode.md
    Original file line number Diff line number Diff line change
    @@ -33,7 +33,6 @@ MESSAGE_CATEGORY = 'Messages'
    def enable_remote_debugging():
    try:
    raise Exception
    import ptvsd
    if ptvsd.is_attached():
    QgsMessageLog.logMessage("Remote Debug for Visual Studio is already active", MESSAGE_CATEGORY, Qgis.Info)
  6. veuncent renamed this gist Oct 11, 2019. 1 changed file with 0 additions and 0 deletions.
  7. veuncent created this gist Oct 11, 2019.
    61 changes: 61 additions & 0 deletions RemoteDebuggingQgisVsCode
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,61 @@
    1. Install `ptvsd` using the same Python interpreter Qgis uses.
    E.g. on Windows:
    ```
    C:\OSGeo4W64\apps\Python37\python.exe -m pip install ptvsd==4.3.2
    ```

    2. Add a debug configuration to `launch.json`:
    ```
    {
    "name": "Python: Remote Attach",
    "type": "python",
    "request": "attach",
    "port": 5678,
    "host": "localhost",
    "pathMappings": [
    {
    "localRoot": "${workspaceFolder}",
    "remoteRoot": "${workspaceFolder}"
    }
    ]
    }
    ```

    3. Add code that attaches the debugger when your plugin launches:
    ```
    import sys
    import traceback
    from qgis.core import QgsMessageLog, Qgis


    MESSAGE_CATEGORY = 'Messages'


    def enable_remote_debugging():
    try:
    raise Exception
    import ptvsd
    if ptvsd.is_attached():
    QgsMessageLog.logMessage("Remote Debug for Visual Studio is already active", MESSAGE_CATEGORY, Qgis.Info)
    return
    ptvsd.enable_attach(address=('localhost', 5678))
    QgsMessageLog.logMessage("Attached remote Debug for Visual Studio", MESSAGE_CATEGORY, Qgis.Info)
    except Exception as e:
    exc_type, exc_value, exc_traceback = sys.exc_info()
    format_exception = traceback.format_exception(exc_type, exc_value, exc_traceback)
    QgsMessageLog.logMessage(repr(format_exception[0]), MESSAGE_CATEGORY, Qgis.Critical)
    QgsMessageLog.logMessage(repr(format_exception[1]), MESSAGE_CATEGORY, Qgis.Critical)
    QgsMessageLog.logMessage(repr(format_exception[2]), MESSAGE_CATEGORY, Qgis.Critical)

    class MyQgisPlugin:
    def __init__(self, iface):
    enable_remote_debugging()
    # ... the rest of your plugin code
    ```

    4. In VS Code start debugging using the Python: Remote Attach configuration defined above


    ## Acknowledgement
    This gist is based on one written by [AsgerPeterson](https://gist.github.com/AsgerPetersen/9ea79ae4139f4977c31dd6ede2297f90).
    Instead of installing a third-party Qgis plugin for attaching the debugger, I added the code in point 3.