An extension for nvim-dap providing default configurations for python and methods to debug individual test methods or classes.
- Requires Neovim >= 0.5
- Requires nvim-dap
- Requires debugpy
- Install like any other neovim plugin:
- If using vim-plug:
Plug 'mfussenegger/nvim-dap-python' - If using packer.nvim:
use 'mfussenegger/nvim-dap-python'
- If using vim-plug:
If you want to use the test runner functionality, it additionally requires a tree sitter parser for Python.
It is recommended to install debugpy into a dedicated virtualenv. To do so:
mkdir .virtualenvs
cd .virtualenvs
python -m venv debugpy
debugpy/bin/python -m pip install debugpyThe debugger will automatically pick-up another virtual environment if it is activated before neovim is started.
Install either:
- Via
:TSInstall pythonof nvim-treesitter - Compile the parser from tree-sitter-python and copy it into
.config/nvim/parser/:git clone https://github.com/tree-sitter/tree-sitter-python.gitcd tree-sitter-pythoncc -O2 -o ~/.config/nvim/parser/python}.so -I./src src/parser.c src/scanner.cc -shared -Os -lstdc++ -fPIC
- Call
setupin yourinit.vimto register the adapter and configurations:
lua require('dap-python').setup('~/.virtualenvs/debugpy/bin/python')The argument to setup is the path to the python installation which contains the debugpy module.
- Use nvim-dap as usual.
- Call
:lua require('dap').continue()to start debugging. - See
:help dap-mappingsand:help dap-api. - Use
:lua require('dap-python').test_method()to debug the closest method above the cursor.
Supported test frameworks are unittest and pytest. It defaults to using
unittest. To configure pytest set the test runner like this:
lua require('dap-python').test_runner = 'pytest'nnoremap <silent> <leader>dn :lua require('dap-python').test_method()<CR>
nnoremap <silent> <leader>df :lua require('dap-python').test_class()<CR>
vnoremap <silent> <leader>ds <ESC>:lua require('dap-python').debug_selection()<CR>If you call the require('dap-python').setup method it will create a few nvim-dap configuration entries. These configurations are general purpose configurations suitable for many use cases, but you may need to customize the configurations - for example if you want to use Docker containers.
To add your own entries, you can extend the dap.configurations.python list after calling the setup function:
lua << EOF
require('dap-python').setup('/path/to/python')
table.insert(require('dap').configurations.python, {
type = 'python',
request = 'launch',
name = 'My custom launch configuration',
program = '${file}',
-- ... more options, see https://github.com/microsoft/debugpy/wiki/Debug-configuration-settings
})
EOFAn alternative is to use project specific .vscode/launch.json files, see :help dap-launch.json.
The Debugpy Wiki contains a list of all supported configuration options.
A test runner building upon vim-test with nvim-dap support. Aims to work for all python runners.