Project

General

Profile

Debugging Python using vscode

Since we need to set environment variables before calling python, we are going to setup the python script to start the debugger client and wait for vscode to attach remotely.

Ideally we'd figure out a way to just launch the script directly from vscode. TBD

  • Add the following code near the top of the snap.py file
    import debugpy
    debugpy.listen(('localhost', 8888))
    print("Waiting for debugger attach")
    debugpy.wait_for_client()
    print("Debugger attached")
    debugpy.breakpoint()
    
    • Note: This shouldn't be necessary, we should be able to launch the script per below, but this wasn't pausing execution for me...
      python -m debugpy --listen 0.0.0.0:8888 ./snap.py --wait-for-client
      
  • Open folder with snap.py in vscode
  • Run -> Add Configuration...
    • Select Python
    • Select Remote Attach
  • Input the following, note the only changes from the default are a change in port number and changing justMyCode to false
    {
        // Use IntelliSense to learn about possible attributes.
        // Hover to view descriptions of existing attributes.
        // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
        "version": "0.2.0",
        "configurations": [
            {
                "name": "Python: Remote Attach",
                "type": "python",
                "request": "attach",
                "connect": {
                    "host": "localhost",
                    "port": 8888
                },
                "pathMappings": [
                    {
                        "localRoot": "${workspaceFolder}",
                        "remoteRoot": "." 
                    }
                ],
                "justMyCode": false
            }
        ]
    }
    
  • Run snap.py
    python snap.py
    ...
    Waiting for debugger attach
    
  • In vscode: ctrl-shift-D to open the Run and Debug pane
  • Press the play button next to the "Python: Remote Attach" launch job we just created
  • You should now be attached in the debugger and be able to step through the code

Go to top
Add picture from clipboard (Maximum size: 1 GB)