Skip to content

Latest commit

 

History

History
 
 

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

layout default
title How to Debug Python with VSCode
permalink /python/

How to Debug Python with VSCode

Summary

Basic

Spec

  • OS
    • ✅ MacOS
    • ✅ Windows
    • ✅ Linux
  • Break Point
    • ✅ break point
    • ❓ condition break point : it does not work on my machine, always breaks
    • ❌ function breakpoint
    • ✅ uncaught exception breakpoint
    • ✅ all exception breakpoint
  • Step Execution
    • ✅ Step Over
    • ✅ Step Into
    • ✅ Step Out
    • ✅ Continue
  • Variables
    • ✅ variables views
    • ✅ watch variables
  • Call Stack
    • ✅ call stack
  • Evaluation
    • ✅ eval expression to show variables
    • ✅ eval expression to change variables
  • Type of Execution
    • ✅ debug unit test
    • ✅ debug executable package
    • ❓ remote debugging

Instraction

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 VSCode and retry.

use launch.json

Menu: Python:Python module

DebugPythonModule

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Python Module",
      "type": "python",
      "request": "launch",
      "stopOnEntry": true,
      "pythonPath": "${config:python.pythonPath}",
      "module": "unittest",
      "args": [
        // test package
        // <test_file>
        // <test_file>.<test_class>
        // <test_file>.<test_class>.<test_method>
        "test_bubble_sort.TestBubbleSort.test_bubble_sort"
      ],
      "cwd": "${workspaceRoot}",
      "env": {},
      "envFile": "${workspaceRoot}/.env",
      "debugOptions": [
        "WaitOnAbnormalExit",
        "WaitOnNormalExit",
        "RedirectOutput"
      ]
    }
  ]
}

debug with integrated terminal

Menu: Python: Python program with Integrated Terminal/Console

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Integrated Terminal/Console",
      "type": "python",
      "request": "launch",
      "stopOnEntry": true,
      "pythonPath": "${config:python.pythonPath}",
      "program": "bubble_sorter.py",
      "cwd": "",
      "console": "integratedTerminal",
      "env": {},
      "envFile": "${workspaceRoot}/.env",
      "debugOptions": [
        "WaitOnAbnormalExit",
        "WaitOnNormalExit"
      ]
    }
  ]
}

executable file debug

executable file: bubble_sorter.py

note: DebugPython

launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Python",
      "type": "python",
      "request": "launch",
      "stopOnEntry": true,
      "pythonPath": "${config:python.pythonPath}",
      //"program": "${file}",
      "program": "bubble_sorter.py",
      "args": [
        "4",
        "3",
        "2",
        "1"
      ],
      "cwd": "${workspaceRoot}",
      "env": {},
      "envFile": "${workspaceRoot}/.env",
      "debugOptions": [
        "WaitOnAbnormalExit",
        "WaitOnNormalExit",
        "RedirectOutput"
      ]
    }
  ]
}

remote debug

TODO: not work on my machine!

prepare

install pyvsd package

pip install ptvsd

code

add remote debug code.

import ptvsd

ptvsd.enable_attach("nnyn", address=('0.0.0.0', 2345))
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",
			"localRoot": "${workspaceRoot}",
			"remoteRoot": "${workspaceRoot}",
			"port": 3000,
			"secret": "my_secret",
			"host": "localhost"
		}
	]
}

Dojango Application

TODO: Django Application

Google App Engine

TODO: Google App Engine Application