[Q] How to use this with Django?
yujinyuz opened this issue · comments
I managed to make it work with a simple python file but I was wondering if it would be possible to use this with Django?
And if it is, what would be a sample configuration?
Thanks!
One option would be to launch manage.py runserver
using debugpy
in a terminal and then from within neovim use the Attach remote
configuration.
python -m debugpy --listen 8888 --wait-for-client manage.py runserver
The Attach remote
configuration will prompt for host (which should be set to 127.0.0.1
and for the port (Should be set to your --listen
argument - e.g. 8888)
An alternative might be to add a configuration like this:
table.insert(dap.configurations.python, {
type = 'python',
request = 'launch',
name = 'Django',
program = vim.fn.getcwd() .. '/manage.py', -- NOTE: Adapt path to manage.py as needed
args = {'runserver'},
})
@yujinyuz Did this answer your question?
If somebody comes here looking to make it run with Django, I had to add the noreload option to the command:
python -m debugpy --listen 5678 --wait-for-client manage.py runserver --noreload
I'm trying to get the alternative version to work because it's a hassle to restart the attached version if one made changes to the code. (I also tried to use a non relative path to the manage.py file but this didn't help)
However it looks like no process is starting if I run the Django launch command with the configuration from above. Is there a way to get some logging about the debugging process to check if there are any error messages?
An alternative might be to add a configuration like this:
table.insert(dap.configurations.python, { type = 'python', request = 'launch', name = 'Django', program = vim.fn.getcwd() .. '/manage.py', -- NOTE: Adapt path to manage.py as needed args = {'runserver'}, })
This configuration doesn't work, the debugger works but nothing appears in the dap-repl
window
Error executing vim.schedule lua callback: .../ms/.config/nvim/plugins/nvim-dap-ui/lua/dapui/state.lua:119: attempt to index fi
eld 'current_frame' (a nil value)
stack traceback:
.../ms/.config/nvim/plugins/nvim-dap-ui/lua/dapui/state.lua:119: in function '_refresh_scopes'
.../ms/.config/nvim/plugins/nvim-dap-ui/lua/dapui/state.lua:101: in function 'c'
/home/ms/.config/nvim/plugins/nvim-dap/lua/dap/session.lua:579: in function </home/ms/.config/nvim/plugins/nvim-dap/lua
/dap/session.lua:573>
Something went wrong here, I can't figure out what it is. Also, the configuration that works with VSCode when I use it with require('dap.ext.vscode').load_launchjs()
do the same weird thing.
The error you posted is coming from nvim-dap-ui. Can you disable that and try again?
The error you posted is coming from nvim-dap-ui. Can you disable that and try again?
Yes I know, this I think because the debugger didn't start, so nvim-dap-ui couldn't find the current_frame
.
I tried without nvim-dap-ui, now I can evaluate expressions in the repl but there is django didn't started the server and ping
ing the django backend server says:
╰─➤ ping http://localhost:8000
ping: http://localhost:8000: Name or service not known
The only way I manage to run the debugger is with "Attach remote", but I wish to quickly start debugging using configurations just by pressing some keybindings in my neovim.
I now need to run manage.py
in another venv than the debugpy
env, or must I install debugpy in the same env?
~/.virtualenvs/debugpy/bin/python -m debugpy --listen 5678 --wait-for-client manage.py runserver
OK, here we go, no need for Attach to remote
:
table.insert(dap.configurations.python, {
type = 'python',
request = 'launch',
name = 'Django',
program = vim.fn.getcwd() .. '/manage.py',
args = {'runserver', '--noreload'},
})
If somebody comes here looking to make it run with Django, I had to add the noreload option to the command:
python -m debugpy --listen 5678 --wait-for-client manage.py runserver --noreload
This is the trick... Alhamdulillah, it is working after too much time wasted on how to debug my django code.
Hi, can someone provide more info about how to run debugger like that?
I have put the table on my config, but when I run lua require('dap').continue() with my keybind, I'm not able to select other than 1.file, 2.file with argument, 3.remote. I can't find the new config basically.
I think you need to use table.insert
. See my comment above, I used it to make new custom config for Django.
@MuhammadSawalhy Can you share your working config file? I'm having an issue, where I'm getting the error No module named 'backend'
from this line -> from backend.models import ...
.
local dap = require('dap')
dap.adapters.python = {
type = 'executable';
command = '~/.virtualenvs/debugpy/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";
-- Options below are for debugpy, see https://github.com/microsoft/debugpy/wiki/Debug-configuration-settings for supported options
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;
},
}
table.insert(dap.configurations.python, {
type = 'python',
request = 'launch',
name = 'Django',
program = vim.fn.getcwd() .. '/manage.py', -- NOTE: Adapt path to manage.py as needed
args = {'runserver', '--noreload'},
})
This is my config file, I've got it working with Attach Remote
. But I want to also be able to use Launch File
Also is there a way to omit the --noreload
flag, because this is pretty handy if one doesn't have to manually restart everything after some changes.
No module named 'backend'
from this line ->from backend.models import ...
.
I think you need to activate your virtual environment in the terminal before opening NeoVim.
This is my config file, I've got it working with
Attach Remote
. But I want to also be able to useLaunch File
You should choose Django
not Launh File
because this is how we named the Django dap configuration.
My virtual environment is activated in the terminal, also I don't get an option django
when I try to launch DAP
Anyone have insight on where this config goes? All the user config files I found in astronvim are of the format:
return {
...stuff...
}
and I don't see where I can add the table.insert configuration information. Appreciate the help up front, I know its probably a dumb question but I've been fiddling with it for a bit now.
@JamesonNetworks You can take a look at my dotfiles, I got it working. You just need to make sure that you use this config to launch your django instance. And not try to activate this while you have the django backend server running using python manage.py runserver
https://github.com/DMunkei/Dotfiles/blob/master/.config/nvim/lua/dmunkei/debugger/python.lua
@DMunkei Thanks for the pointer to those dotfiles, it was very helpful! My problem ended up being that I hadn't installed nvim-dap-python at all. I was getting started with astronvim and missed that step. After I installed the adapter, everything started working.
It would let me create and pickup the launch.json config without it, but the pydebug step would hang while trying to attach. After installing the connector using the user customization template in astronvim my problem was solved.
An updated code as various references in this ticket aren't working anymore with more details:
local pythonPath = function()
local cwd = vim.loop.cwd()
if vim.fn.executable(cwd .. '/.venv/bin/python') == 1 then
return cwd .. '/.venv/bin/python'
else
return '/usr/bin/python'
end
end
local set_python_dap = function()
require('dap-python').setup() -- earlier so setup the various defaults ready to be replaced
dap.configurations.python = {
{
type = 'python';
request = 'launch';
name = "Launch file";
program = "${file}";
pythonPath = pythonPath()
},
{
type = 'python',
request = 'launch',
name = 'DAP Django',
program = vim.loop.cwd() .. '/manage.py',
args = {'runserver', '--noreload'},
justMyCode = true,
django = true,
console = "integratedTerminal",
},
{
type = 'python';
request = 'attach';
name = 'Attach remote';
connect = function()
return {
host = '127.0.0.1',
port = 5678
}
end;
},
{
type = 'python';
request = 'launch';
name = 'Launch file with arguments';
program = '${file}';
args = function()
local args_string = vim.fn.input('Arguments: ')
return vim.split(args_string, " +")
end;
console = "integratedTerminal",
pythonPath = pythonPath()
}
}
dap.adapters.python = {
type = 'executable',
command = pythonPath(),
args = {'-m', 'debugpy.adapter'}
}
end
set_python_dap()
vim.api.nvim_create_autocmd({"DirChanged"}, {
callback = function() set_python_dap() end,
})
With this code:
- If the folder changes updated the python path
- this plugin is setup before of everything in this way sets all the defaults
- Readd all the various run mode using the
pythonPath
function in this way will use the right python version - To run everything (as it isn't clear in the ticket with various comments), you just need to run from neovim "DAP Django" that will run Django from the right environment with debugpy and will write in the dap-ui (if you have installed it) the log
- Django launcher, use the 2 parameters one to set that is django (yes debugpy has a parameter for that) and to not debug the libraries but just my code
I tested with poetry (configured for the folder on cwd) and works but in case you just need to adapt the pythonPath function.
Right now works in the way that you start from neovim but on changes you need to restart the DAP, I am trying to understand if subProcess
parameter for debugpy is supported on this project that should allow the live reload #21