Skip to content
Arthur Greef edited this page Feb 23, 2018 · 10 revisions

Debugging Python Code

Remote debugging with Visual Studio Code on Windows

Install the following products on your Windows host to remote debug with VS Code.

  • Install Docker for Windows (D4W)
  • Enable Windows Subsystem for Linus (WSL).
  • Intall VSCode
  • Install Python

The PTVSD python module is installed on the -dev- images. If you need to manually install it then run the following command. vtvsd hangs on the ptvsd.wait_for_attach() statement if you use ptvsd versions later than 3.0.0.0

pip3 install ptvsd==3.0.0.0

VSCode and PTVSD communicate over ports that need to be exposed by containers. The docker compose files with vscode in the name can be used to expose ports on containers. If you need to expose ports yourself then add the following to a compose file. You can also expose and map ports using docker run command line options.

services:
  shell:
    expose:
      - 5678
    ports:
      - "5678:5678"

For example, to bring up a network of services with ports exposes run the following command. Executing 'docker ps' or 'docker inspect ' will display the exposed ports.

docker-compose -f docker/compose/hashblock-local.yaml up

In a separate bash shell you can shell into a container and run python using the following command.

docker exec -it <container name> bash -c python3

Execute the following python statements to start the python debugger. The port name needs to be the number of the port that has been exposed on the container that is the target of the previous bash shell.

import ptvsd
ptvsd.enable_attach('secret',('0.0.0.0',5678))
ptvsd.wait_for_attach()

If PIP is not installed on the container then run the following commands to install PIP and ptvsd.

python3 ./sdk/python/get-pip.py
pip3 install ptvsd==3.0.0.0

In VSCode select python and settings and edit the python attach setting.

        {
            "name": "Python: Attach",
            "type": "python",
            "request": "attach",
            "localRoot": "${workspaceFolder}",
            "remoteRoot": "/project/sawtooth-eventchain",
            "port": 5678,
            "secret": "secret",
            "host": "localhost"
        },

Run the python debugger with the python attach configuration in VSCode. If you get no connection or startup errors then you can connect to the remote debugger. Now run the test on the container and set a breakpoint in the test in VSCode.