How to configure nvim-dap-python to pass a different working directory #919
-
Hello, Sorry, I am still quite new to DAP. I am using
But I don't know how to call it. Please let me know. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
I am too confused about this - it seems like a "skeleton" for a configuration you can pass to However, if you simply have some dap.configurations = {
python = {
{
type = 'python';
request = 'launch';
name = "Launch file";
program = "${file}";
pythonPath = 'python';
cwd = "/"
}
}
} I too personally use mostly debugpy for simple Python scripts, and the complexity of these configurations has made me write the following configuration: vim.api.nvim_create_user_command("RunScriptWithArgs", function(t)
-- :help nvim_create_user_command
args = vim.split(vim.fn.expand(t.args), '\n')
approval = vim.fn.confirm(
"Will try to run:\n " ..
vim.bo.filetype .. " " ..
vim.fn.expand('%') .. " " ..
t.args .. "\n\n" ..
"Do you approve? ",
"&Yes\n&No", 1
)
if approval == 1 then
dap.run({
type = vim.bo.filetype,
request = 'launch',
name = 'Launch file with custom arguments (adhoc)',
program = '${file}',
args = args,
})
end
end, {
complete = 'file',
nargs = '*'
})
vim.keymap.set('n', '<leader>R', ":RunScriptWithArgs ") You can adapt the above to make the |
Beta Was this translation helpful? Give feedback.
-
Thanks for this! I actually stopped using lua << EOF
local dap = require('dap')
dap.adapters.python = {
type = 'executable';
command = os.getenv("VIRTUAL_ENV") .. "/bin/python";
args = { '-m', 'debugpy.adapter' };
}
dap.configurations.python = {
{
-- The first three options are required by nvim-dap
type = 'python'; -- the type here established the link to the adapter definition: `dap.adapters.python`
request = 'launch';
name = "Launch file";
cwd = "/path/to/repository/root/dir"; --python is executed from this directory
program = "${file}"; -- This configuration will launch the current file if used.
pythonPath = function()
-- debugpy supports launching an application with a different interpreter then the one used to launch debugpy itself.
-- The code below looks for a `venv` or `.venv` folder in the current directly and uses the python within.
-- You could adapt this - to for example use the `VIRTUAL_ENV` environment variable.
local cwd = vim.fn.getcwd()
if vim.fn.executable(cwd .. '/venv/bin/python') == 1 then
return cwd .. '/venv/bin/python'
elseif vim.fn.executable(cwd .. '/.venv/bin/python') == 1 then
return cwd .. '/.venv/bin/python'
else
return '/usr/bin/python'
end
end;
},
}
EOF This appears to be doing what I want, but I will review your suggestion and see how much I can incorporate. The interface is certainly very configurable. |
Beta Was this translation helpful? Give feedback.
Thanks for this! I actually stopped using
nvim-dap-python
, as the wiki example was sufficient for what I needed. I currently use the following.nvimrc
that sits at the root directory of my project: