Python debugging is ready out-of-the-box with DAP (Debug Adapter Protocol).
- Set a breakpoint: Navigate to line, press
<leader>Db - Start debugging: Press
<leader>Dc, select "Launch file" - Debug: Use step commands (
<leader>Di,<leader>Dj,<leader>Do)
| Key | Description |
|---|---|
<leader>Db |
Toggle breakpoint |
<leader>DB |
Set conditional breakpoint |
<leader>Dl |
Set log point |
<leader>Dc |
Start/Continue debugging |
<leader>Dr |
Restart debugging |
<leader>Dt |
Terminate debugging |
<leader>Di |
Step into |
<leader>Dj |
Step over |
<leader>Do |
Step out |
<leader>Du |
Toggle DAP UI |
<leader>De |
Evaluate expression |
# example.py
def calculate(x, y):
result = x + y # <- Set breakpoint here (<leader>Db)
return result * 2
def main():
numbers = [1, 2, 3, 4, 5]
for num in numbers:
value = calculate(num, 10) # <- Another breakpoint
print(f"Result: {value}")
if __name__ == "__main__":
main()- Press
<leader>Dc - Select "Launch file"
- DAP UI opens automatically
- When stopped at breakpoint: Inspect variables in DAP UI
- Step through code:
<leader>Di(into),<leader>Dj(over) - Evaluate expressions: Select code →
<leader>De - Continue execution:
<leader>Dc
- 🔴 Breakpoint - Red circle in sign column
- 🟡 Conditional Breakpoint - Yellow circle
- 📝 Log Point - Notebook icon
- ➡️ Current Position - Arrow showing execution point
- Press
<leader>DB - Enter condition:
x > 10 - Breakpoint only triggers when condition is true
- Press
<leader>Dl - Enter message:
Value is {x} - Logs message without stopping execution
- Visual mode: Select variable →
<leader>De - DAP Console: Type Python expressions directly
- Variables panel: Expand objects to see properties
Debugger not starting?
- Check Python is in PATH:
which python3 - Verify debugpy installed:
python3 -m pip install debugpy - Run
:checkhealthto verify DAP setup
Breakpoints not working?
- Ensure file is saved before debugging
- Check breakpoint icon appears in sign column
- Verify Python syntax is correct
Variables not showing?
- Make sure DAP UI is open (
<leader>Du) - Check you're stopped at a breakpoint
- Variables only show in current scope
The debugging setup is pre-configured, but you can customize:
-- In your config (already included)
dap.configurations.python = {
{
type = "python",
request = "launch",
name = "Launch file",
program = "${file}",
pythonPath = function()
return "python3"
end,
},
}- Multi-file projects: Breakpoints work across all Python files
- Virtual environments: Debugger respects your current Python environment
- Import errors: Use step-into to debug module loading issues
- Performance: Use step-over for library code, step-into for your code