Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 31 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,41 @@ tree sitter parser for Python.

### Debugpy

You need to install `debugpy` using either your package manager or via `pip`.
For example:
Options to install [debugpy][3]:

1. Via a system package manager. For example:

```bash
mkdir .virtualenvs
cd .virtualenvs
pacman -S python-debugpy
```

2. Via `pip` in a `venv`:

```bash
mkdir ~/.virtualenvs
cd ~/.virtualenvs
python -m venv debugpy
debugpy/bin/python -m pip install debugpy
```

If you're using virtual environments for your project it will automatically get
picked up if it is active before `nvim` starts, or if it is in a default
location. See [Python dependencies and
Note that the virutalenv used for the `debugpy` can be independent from any
virtualenv you're using in your projects.

`nvim-dap-python` tries to detect your project `venv` and should recognize any
dependencies your project has. See [Python dependencies and
virtualenv](#python-dependencies-and-virtualenv)

### Tree-sitter

If you're using a properly packaged `nvim` version 0.10+, a python tree-sitter
parser should be included and you're all set.
3. Implicit via [uv][uv]

If you're using an older version, or your distribution excluded the parser you
can install it manually using either:
See [Usage](#usage): You need to use `require("dap-python").setup("uv")`


- Via `:TSInstall python` of [nvim-treesitter][4]
### Tree-sitter

To install the python tree-sitter parser you can either:

- Use `:TSInstall python` from [nvim-treesitter][4]
- Compile the parser from [tree-sitter-python][5] and copy it into `.config/nvim/parser/`:
- `git clone https://github.com/tree-sitter/tree-sitter-python.git`
- `cd tree-sitter-python`
Expand All @@ -61,14 +71,20 @@ can install it manually using either:
-- must work in the shell
```

Or if installed globally:
If installed globally:

```lua
require("dap-python").setup("python3")
-- If using the above, then `python3 -m debugpy --version`
-- must work in the shell
```

If using [uv][uv]:

```lua
require("dap-python").setup("uv")
```


2. Use `nvim-dap` as usual.

Expand Down Expand Up @@ -201,3 +217,4 @@ vimcats -f -t lua/dap-python.lua > doc/dap-python.txt
[7]: https://github.com/wbthomason/packer.nvim
[debugpy_wiki]: https://github.com/microsoft/debugpy/wiki/Debug-configuration-settings
[vimcats]: https://github.com/mrcjkb/vimcats
[uv]: https://docs.astral.sh/uv/
42 changes: 31 additions & 11 deletions lua/dap-python.lua
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,9 @@ end


--- Register the python debug adapter
---@param python_path string|nil Path to the python interpreter. Path must be absolute or in $PATH and needs to have the debugpy package installed. Default is `python3`
---
---@param python_path "python"|"python3"|"uv"|string|nil Path to python interpreter. Must be in $PATH or an absolute path and needs to have the debugpy package installed. Defaults to `python3`.
--- If `uv` then debugpy is launched via `uv run`
---@param opts? dap-python.setup.opts See |dap-python.setup.opts|
function M.setup(python_path, opts)
local dap = load_dap()
Expand All @@ -219,25 +221,43 @@ function M.setup(python_path, opts)
local port = (config.connect or config).port
---@diagnostic disable-next-line: undefined-field
local host = (config.connect or config).host or '127.0.0.1'
cb({

---@type dap.ServerAdapter
local adapter = {
type = 'server',
port = assert(port, '`connect.port` is required for a python `attach` configuration'),
host = host,
enrich_config = enrich_config,
options = {
source_filetype = 'python',
}
})
}
cb(adapter)
else
cb({
type = 'executable';
command = python_path;
args = { '-m', 'debugpy.adapter' };
enrich_config = enrich_config;
options = {
source_filetype = 'python',
---@type dap.ExecutableAdapter
local adapter
if python_path == "uv" then
adapter = {
type = "executable",
command = "uv",
args = {"run", "--with", "debugpy", "python", "-m", "debugpy.adapter"},
enrich_config = enrich_config,
options = {
source_filetype = "python"
}
}
else
adapter = {
type = "executable",
command = python_path,
args = {"-m", "debugpy.adapter"};
enrich_config = enrich_config,
options = {
source_filetype = "python"
}
}
})
end
cb(adapter)
end
end
dap.adapters.debugpy = dap.adapters.python
Expand Down
Loading