Thanks to this video for a cool overview of nvim-dap https://www.youtube.com/watch?v=D7XPedIn4G4
I recently went down a deeper rabbit hole than expected trying to debug python code in neovim (nvim) using nvim-dap while running my projects via uv.
Historically I have used pdb++ for python debugging (with nvim shortcuts for toggling breakpoint()), but I wanted to try out nvim-dap.
The problem
My usual workflow is to edit files in nvim and run them with
uv run path/to/script.py
However, this fails in codebases where debugpy is not installed. A common workaround is to install debugpy globally (not ideal) and run:
uv run --with debugpy path/to/script.py
But manually toggling between these two scenarios is incovenient.
The solution
The correct approach is:
- Check if the project venv has
debugpy - If yes, run nvim-dap directly inside the venv
- If no, run with
uv --with debugpy
nvim-dap adapter configuration
local function python_adapter()
vim.fn.system({ 'uv', 'run', 'python', '-c', 'import debugpy' })
if vim.v.shell_error == 0 then
local python = vim.fn.trim(vim.fn.system({ 'uv', 'run', 'python', '-c', 'import sys; print(sys.executable)' }))
return { type = 'executable', command = python, args = { '-m', 'debugpy.adapter' } }
end
return { type = 'executable', command = 'uv', args = { '--with', 'debugpy', 'run', 'python', '-m', 'debugpy.adapter' } }
end
dap.adapters.python = python_adapter()
Launching programs
preLaunchTask = function()
return 'uv run ' .. vim.fn.expand('${file}')
end
Full config
My full config can be found here