| layout | default |
|---|---|
| title | How to Debug Python with VSCode |
| permalink | /python/ |
- Python.org
- Extension: Python
- Debugger: Python
- module code: bubble_sort.py
- 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
Only installing Extension.
If you want to pyenv or other environment tools, select your environment with belong way.
F1->Python: Select Workspace Interpreter
- test code: test_bubble_sort.py
F1->Python: Run Current Unit Test File
Enable and configure a Test Framework.->select your test framework.
sample: unittest->.->test_*
Codelens on test function shows run and debug test button.
But it sometimes doesn't start debug in my machine. Then, it restarts VSCode and retry.
Menu: Python:Python module
{
"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"
]
}
]
}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: bubble_sorter.py
{
"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"
]
}
]
}TODO: not work on my machine!
install pyvsd package
pip install ptvsdadd 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{
"version": "0.2.0",
"configurations": [
{
"name": "Attach (Remote Debug)",
"type": "python",
"request": "attach",
"localRoot": "${workspaceRoot}",
"remoteRoot": "${workspaceRoot}",
"port": 3000,
"secret": "my_secret",
"host": "localhost"
}
]
}TODO: Django Application
TODO: Google App Engine Application









