Skip to content

Instantly share code, notes, and snippets.

@vfdev-5
Last active October 5, 2021 09:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vfdev-5/558a80ac5c939e1d72ed45b63a327124 to your computer and use it in GitHub Desktop.
Save vfdev-5/558a80ac5c939e1d72ed45b63a327124 to your computer and use it in GitHub Desktop.
Remote debugging python code with VSCode

My two cents on debugging, if you have ssh access to some of your nodes, especially the one where an xp failed. There is also a possibility to remotely debug the code. This may seem a bit tricky however could help to check the pipeline. I tested it with VSCode.

  • ssh to the machine and the environment (docker container etc)

Using debugpy

  1. Install debugpy
pip install debugpy
  1. Setup debug configuration on VSCode (https://code.visualstudio.com/docs/python/debugging#_remote-debugging)
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Remote Attach",
            "type": "python",
            "request": "attach",
            "connect": {
                "host": "localhost",
                "port": 5678
            },
            "pathMappings": [
                {
                    "localRoot": "${workspaceFolder}",
                    "remoteRoot": "."
                }
            ]
        }
    ]
}
  1. Run
python -m debugpy --wait-for-client --listen 5678 script.py
  1. In VSCode hit F5
  2. Profit 🙂

Using ptvsd

  1. Setup pip install --upgrade ptvsd on local and remote machines
  2. Follow the guide on how to setup debugging interface from VSCode: https://code.visualstudio.com/docs/python/debugging#_remote-debugging
{
  "name": "Python: Attach",
  "type": "python",
  "request": "attach",
  "port": 1313,
  "host": "localhost",
  "pathMappings": [
    {
      "localRoot": "${workspaceFolder}", // Maps C:\Users\user1\project1
      "remoteRoot": "." // To current working directory ~/project1
    }
  ]
}
  1. On remote run: python -m ptvsd --host 0.0.0.0 --port 1313 --wait $PWD/code_to_debug.py
  2. On local machine: Set some breakpoints, run debug configuration "Attach (Remote Debug)".
  3. Profit 🙂

HTH

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