Skip to content

Commit 474d0f6

Browse files
committed
Support running debugpy via uv run
1 parent 9a9efa0 commit 474d0f6

File tree

2 files changed

+61
-19
lines changed

2 files changed

+61
-19
lines changed

README.md

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,36 @@ tree sitter parser for Python.
1818

1919
### Debugpy
2020

21-
You need to install `debugpy` using either your package manager or via `pip`.
22-
For example:
21+
Options to install [debugpy][3]:
22+
23+
1. Via a system package manager. For example:
2324

2425
```bash
25-
mkdir .virtualenvs
26-
cd .virtualenvs
26+
pacman -S python-debugpy
27+
```
28+
29+
2. Via `pip` in a `venv`:
30+
31+
```bash
32+
mkdir ~/.virtualenvs
33+
cd ~/.virtualenvs
2734
python -m venv debugpy
2835
debugpy/bin/python -m pip install debugpy
2936
```
3037

31-
If you're using virtual environments for your project it will automatically get
32-
picked up if it is active before `nvim` starts, or if it is in a default
33-
location. See [Python dependencies and
38+
Note that the virutalenv used for the `debugpy` can be independent from any
39+
virtualenv you're using in your projects.
40+
41+
`nvim-dap-python` tries to detect your project `venv` and should recognize any
42+
dependencies your project has. See [Python dependencies and
3443
virtualenv](#python-dependencies-and-virtualenv)
3544

45+
46+
3. Implicit via [uv][uv]
47+
48+
See [Usage](#Usage): You need to use `require("dap-python").setup("uv")`
49+
50+
3651
### Tree-sitter
3752

3853
If you're using a properly packaged `nvim` version 0.10+, a python tree-sitter
@@ -61,14 +76,20 @@ can install it manually using either:
6176
-- must work in the shell
6277
```
6378

64-
Or if installed globally:
79+
If installed globally:
6580

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

87+
If using [uv][uv]:
88+
89+
```lua
90+
require("dap-python").setup("uv")
91+
```
92+
7293

7394
2. Use `nvim-dap` as usual.
7495

@@ -201,3 +222,4 @@ vimcats -f -t lua/dap-python.lua > doc/dap-python.txt
201222
[7]: https://github.com/wbthomason/packer.nvim
202223
[debugpy_wiki]: https://github.com/microsoft/debugpy/wiki/Debug-configuration-settings
203224
[vimcats]: https://github.com/mrcjkb/vimcats
225+
[uv]: https://docs.astral.sh/uv/

lua/dap-python.lua

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,9 @@ end
207207

208208

209209
--- Register the python debug adapter
210-
---@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`
210+
---
211+
---@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`.
212+
--- If `uv` then debugpy is launched via `uv run`
211213
---@param opts? dap-python.setup.opts See |dap-python.setup.opts|
212214
function M.setup(python_path, opts)
213215
local dap = load_dap()
@@ -219,25 +221,43 @@ function M.setup(python_path, opts)
219221
local port = (config.connect or config).port
220222
---@diagnostic disable-next-line: undefined-field
221223
local host = (config.connect or config).host or '127.0.0.1'
222-
cb({
224+
225+
---@type dap.ServerAdapter
226+
local adapter = {
223227
type = 'server',
224228
port = assert(port, '`connect.port` is required for a python `attach` configuration'),
225229
host = host,
226230
enrich_config = enrich_config,
227231
options = {
228232
source_filetype = 'python',
229233
}
230-
})
234+
}
235+
cb(adapter)
231236
else
232-
cb({
233-
type = 'executable';
234-
command = python_path;
235-
args = { '-m', 'debugpy.adapter' };
236-
enrich_config = enrich_config;
237-
options = {
238-
source_filetype = 'python',
237+
---@type dap.ExecutableAdapter
238+
local adapter
239+
if python_path == "uv" then
240+
adapter = {
241+
type = "executable",
242+
command = "uv",
243+
args = {"run", "--with", "debugpy", "python", "-m", "debugpy.adapter"},
244+
enrich_config = enrich_config,
245+
options = {
246+
source_filetype = "python"
247+
}
248+
}
249+
else
250+
adapter = {
251+
type = "executable",
252+
command = python_path,
253+
args = {"-m", "debugpy.adapter"};
254+
enrich_config = enrich_config,
255+
options = {
256+
source_filetype = "python"
257+
}
239258
}
240-
})
259+
end
260+
cb(adapter)
241261
end
242262
end
243263
dap.adapters.debugpy = dap.adapters.python

0 commit comments

Comments
 (0)