pocco81 / dap-buddy.nvim

🐞 Debug Adapter Protocol manager for Neovim
GNU General Public License v3.0
400 stars 48 forks source link

Use system package in debugpy #48

Closed meijieru closed 2 years ago

meijieru commented 2 years ago

Currently, I have to manually instead of the dependences for the python within the environment. Any tips on how to make it works with system packages?

pocco81 commented 2 years ago

What kind of dependencies are we talking about? Could you elaborate?

meijieru commented 2 years ago

For example, I have the package CloudFlare installed by the package manager. However, the debugpy could not find it as debugpy is installed in virtualenv. So when I start debugging, it complains about the missing packages.

meijieru commented 2 years ago

Solved by overriding the pythonPath function

pocco81 commented 2 years ago

Hey @meijieru! do you mind sharing what you changed it to? Perhaps we could make a FAQ out of this.

meijieru commented 2 years ago

The default pythonPath fall back to the virtualenv where debugpy is installed. Here I just override it to ignore that path.

function()
  local dap_install = require "dap-install"
  local dbg_list = require("dap-install.api.debuggers").get_installed_debuggers()

  local overrides = {
    python = {
      adapters = {
        type = "executable",
        command = "python3",
        args = { "-m", "debugpy.adapter" },
      },
      configurations = {
        {
          type = "python",
          request = "launch",
          name = "Launch file",
          program = "${file}",
          pythonPath = function()
            local venv_path = os.getenv "VIRTUAL_ENV"
            if venv_path then
              local util_sys = require "dap-install.utils.sys"
              if util_sys.is_windows() then
                return venv_path .. "\\Scripts\\python.exe"
              end
              return venv_path .. "/bin/python"
            end

            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/python3"
            end
          end,
        },
      },
    },
  }

  for _, debugger in ipairs(dbg_list) do
    dap_install.config(debugger, overrides[debugger])
  end
end