How to Debug Python with VS Code

Summary

Basic

Spec

Instruction

Only installing Extension.

additional

If you want to pyenv or other environment tools, select your environment with belong way.

F1->Python: Select Workspace Interpreter

SelectWorkspaceInterpreter1.png

SelectWorkspaceInterpreter2.png

unit test

inline

F1->Python: Run Current Unit Test File

CodelensUnitTest1.png

Enable and configure a Test Framework.->select your test framework.

CodelensUnitTest2.png CodelensUnitTest3.png

sample: unittest->.->test_*

CodelensUnitTest4.png CodelensUnitTest5.png

Codelens on test function shows run and debug test button.

CodelensUnitTest6.png

But it sometimes doesn’t start debug in my machine. Then, it restarts VS Code and retry.

use launch.json

Menu: Python:Python module

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Python Module",
      "type": "python",
      "request": "launch",
      "module": "unittest",
      "args": [
        // test package
        // <test_file>
        // <test_file>.<test_class>
        // <test_file>.<test_class>.<test_method>
        "bubblesort.test.test_bubblesort"
      ]
    }
  ]
}

executable file debug

executable file: main.py

launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Python",
      "type": "python",
      "request": "launch",
      "program": "${workspaceFolder}/main.py",
      "args": ["4", "3", "2", "1"]
    }
  ]
}

execute module debug

module : bubblesort

launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Python",
      "type": "python",
      "request": "launch",
      "module": "bubblesort",
      "args": ["4", "3", "2", "1"]
    }
  ]
}

remote debug

prepare

install pyvsd package

pip install ptvsd

code

add remote debug code.

import ptvsd

ptvsd.enable_attach("nnyn", address=('0.0.0.0', 3333))
ptvsd.wait_for_attach()

run follow command.

python bubble_sorter_for_remote.py 4 3 2 1

launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Attach (Remote Debug)",
      "type": "python",
      "request": "attach",
      "pathMappings": [
        {
          "localRoot": "${workspaceFolder}",
          "remoteRoot": "/home/nnyn/vscode-debug-specs/python"
        }
      ],
      "port": 3333,
      "host": "127.0.0.1"
    }
  ]
}