mfussenegger / nvim-dap-python

An extension for nvim-dap, providing default configurations for python and methods to debug individual test methods or classes.
GNU General Public License v3.0
492 stars 46 forks source link

Not using correct pythonpath? #149

Open emilnewel opened 3 weeks ago

emilnewel commented 3 weeks ago

For some reason DAP can't figure out the correct pythonpath in my project. I can set breakpoints, see the UI, step a bit into my code but then the debugger stop when I hit the following line

from bar.conf import conf

when i run <cmd>echo $PYTHONPATH I get src/foo:src/bar:src/baz.

venv is set correctly as I can import and use packages installed with pip.

Everything works correctly in Pycharm/VSCode but I wanted to try to move everything over to nvim and this is the only piece of the configuration I have left.

My project structure

.
├── README.md
├── pyproject.toml
├── pyrightconfig.json
└── src
    ├── bar
    │   └── bar
    ├── foo
    │   ├── README.md
    │   ├── manage.py
    │   ├── foo
    │   └── requirements.txt
    └── baz
        └── baz

Dap configuration for python

return {
    {
        "mfussenegger/nvim-dap-python",
        config = function()
            local dap_python = require("dap-python")
            dap_python.setup("/Users/emil/.pyenv/versions/nvim-dap/bin/python") -- created seperate venv for debugpy
            dap_python.test_runner = "pytest"

            vim.keymap.set("n", "<leader>dn", dap_python.test_method, {})
            vim.keymap.set("n", "<leader>df", dap_python.test_class, {})
            vim.keymap.set("v", "<leader>ds", dap_python.debug_selection, {})
        end,
    },
    {
        "mfussenegger/nvim-dap",
        dependencies = { "rcarriga/nvim-dap-ui", "nvim-neotest/nvim-nio" },
        config = function()
            local dap, dapui = require("dap"), require("dapui")
            dapui.setup()
            dap.listeners.before.attach.dapui_config = function()
                dapui.open()
            end
            dap.listeners.before.launch.dapui_config = function()
                dapui.open()
            end
            dap.listeners.before.event_terminated.dapui_config = function()
                dapui.close()
            end
            dap.listeners.before.event_exited.dapui_config = function()
                dapui.close()
            end

            dap.configurations.python = {
                {
                    name = "Python: Django",
                    type = "python",
                    request = "launch",
                    program = "${workspaceFolder}/src/foo/manage.py",
                    args = {
                        "runserver",
                    },
                    django = true,
                    justMyCode = true,
                    purpose = {
                        "debug-test",
                    },
                },
            }
            vim.keymap.set("n", "<leader>b", dap.toggle_breakpoint, {})
            vim.keymap.set("n", "<F5>", dap.continue, {})
            vim.keymap.set("n", "<F10>", dap.step_over, {})
            vim.keymap.set("n", "<F11>", dap.step_into, {})
            vim.keymap.set("n", "<F12>", dap.step_out, {})
        end,
    },
}
emilnewel commented 3 weeks ago

Related output from <cmd>DapShowLog

[ INFO ] 2024-06-25T09:06:20Z+0000 ] ...emil/.local/share/nvim/lazy/nvim-dap/lua/dap/session.lua:754 ]  "Telemetry" "ptvsd"
[ INFO ] 2024-06-25T09:06:20Z+0000 ] ...emil/.local/share/nvim/lazy/nvim-dap/lua/dap/session.lua:754 ]  "Telemetry" "debugpy"
[ WARN ] 2024-06-25T09:06:20Z+0000 ] ...emil/.local/share/nvim/lazy/nvim-dap/lua/dap/session.lua:1016 ] "No event handler for " {
  body = {
    sockets = { {
        host = "127.0.0.1",
        internal = false,
        port = 51915
      } }
  },
  event = "debugpySockets",
  seq = 3,
  type = "event"
}
[ WARN ] 2024-06-25T09:06:20Z+0000 ] ...emil/.local/share/nvim/lazy/nvim-dap/lua/dap/session.lua:1016 ] "No event handler for " {
  body = {
    sockets = { {
        host = "127.0.0.1",
        internal = false,
        port = 51915
      }, {
        host = "127.0.0.1",
        internal = true,
        port = 51916
      } }
  },
  event = "debugpySockets",
  seq = 5,
  type = "event"
}
[ WARN ] 2024-06-25T09:06:20Z+0000 ] ...emil/.local/share/nvim/lazy/nvim-dap/lua/dap/session.lua:1016 ] "No event handler for " {
  body = {
    sockets = { {
        host = "127.0.0.1",
        internal = false,
        port = 51915
      }, {
        host = "127.0.0.1",
        internal = true,
        port = 51917
      }, {
        host = "127.0.0.1",
        internal = true,
        port = 51916
      } }
  },
  event = "debugpySockets",
  seq = 6,
  type = "event"
}
[ WARN ] 2024-06-25T09:06:21Z+0000 ] ...emil/.local/share/nvim/lazy/nvim-dap/lua/dap/session.lua:1016 ] "No event handler for " {
  body = {
    sockets = { {
        host = "127.0.0.1",
        internal = false,
        port = 51915
      }, {
        host = "127.0.0.1",
        internal = true,
        port = 51916
      } }
  },
  event = "debugpySockets",
  seq = 7,
  type = "event"
}
[ WARN ] 2024-06-25T09:08:19Z+0000 ] ...emil/.local/share/nvim/lazy/nvim-dap/lua/dap/session.lua:1016 ] "No event handler for " {
  body = {
    sockets = { {
        host = "127.0.0.1",
        internal = false,
        port = 51915
      } }
  },
  event = "debugpySockets",
  seq = 573,
  type = "event"
}
[ WARN ] 2024-06-25T09:08:19Z+0000 ] ...emil/.local/share/nvim/lazy/nvim-dap/lua/dap/session.lua:1016 ] "No event handler for " {
  body = {
    sockets = {}
  },
  event = "debugpySockets",
  seq = 574,
  type = "event"
}
[ WARN ] 2024-06-25T09:08:19Z+0000 ] ...emil/.local/share/nvim/lazy/nvim-dap/lua/dap/session.lua:1016 ] "No event handler for " {
  body = {
    sockets = {}
  },
  event = "debugpySockets",
  seq = 575,
  type = "event"
}
[ ERROR ] 2024-06-25T09:08:20Z+0000 ] ...emil/.local/share/nvim/lazy/nvim-dap/lua/dap/session.lua:1466 ]    "stderr"    {
  args = { "-m", "debugpy.adapter" },
  command = "/Users/emil/.pyenv/versions/nvim-dap/bin/python",
  enrich_config = <function 1>,
  options = {
    source_filetype = "python"
  },
  type = "executable"
}   "Fatal Python error: _enter_buffered_busy: could not acquire lock for <_io.BufferedReader name='<stdin>'> at interpreter shutdown, possibly due to daemon threads\nPython runtime state: finalizing (tstate=0x0000000126e05bf0)\n"
[ ERROR ] 2024-06-25T09:08:20Z+0000 ] ...emil/.local/share/nvim/lazy/nvim-dap/lua/dap/session.lua:1466 ]    "stderr"    {
  args = { "-m", "debugpy.adapter" },
  command = "/Users/emil/.pyenv/versions/nvim-dap/bin/python",
  enrich_config = <function 1>,
  options = {
    source_filetype = "python"
  },
  type = "executable"
}   "\nCurrent thread 0x00000001f7388c00 (most recent call first):\n  <no Python frame>\n"
[ INFO ] 2024-06-25T09:08:20Z+0000 ] ...emil/.local/share/nvim/lazy/nvim-dap/lua/dap/session.lua:1439 ] "Process closed"    98996
bob3000 commented 2 weeks ago

I got the same issue here. The venv doesn't seem to be found, modules installed with pip cannot be imported. When I step further the program crashes with an import error I can see in the UI that the python interpreter installed via pyenv is used directly and not the one from the venv inside the workspace.

I tried to force the use of the interpreter defined in the venv like

        require("dap-python").setup(LazyVim.get_pkg_path("debugpy", "/venv/bin/python"), {
          pythonPath = vim.fn.getcwd() .. "/.venv/bin/python",
        })

When I do that it seem to find the modules installed with pip but then it is unable to import the modules from the very workspace I want to debug. I haven't been working on a python project for some months but I'm quite sure it was working before.