linux-cultist / venv-selector.nvim

Allows selection of python virtual environment from within neovim
MIT License
379 stars 40 forks source link

Set $CONDA_PREFIX instead of $VIRTUAL_ENV for conda environments #87

Closed tianyouzeng closed 6 months ago

tianyouzeng commented 6 months ago

This PR fixes a bug for Windows users with conda environments.

The plugin originally sets the environment variable $VIRTUAL_ENV when activating virtual environments. This variable is used by nvim-dap-python to set the Python interpreter path for debugging. However, the relative path of python interpreter for Windows conda environments is [venv]\python.exe, which is different from other types virtual environments, whose relative path is [venv]\Scripts\python.exe. Thus setting $VIRTUAL_ENV leads to a wrong python path (namely [venv]\Scripts\python.exe) for nvim-dap-python. This can also be seen from the code of nvim-dap-python plugin:

local get_python_path = function()
  local venv_path = os.getenv('VIRTUAL_ENV')
  if venv_path then
    if is_windows() then
      return venv_path .. '\\Scripts\\python.exe'
    end
    return venv_path .. '/bin/python'
  end

  venv_path = os.getenv("CONDA_PREFIX")
  if venv_path then
    if is_windows() then
      return venv_path .. '\\python.exe'
    end
    return venv_path .. '/bin/python'
  end

  ...
end

Therefore, it seems necessary to set $CONDA_PREFIX instead of $VIRTUAL_ENV when activating conda environments on Windows.

This PR fixes the above issue. It does not introduce any changes to non-Windows users.

linux-cultist commented 6 months ago

Thank you! Let me have a look this weekend to make sure it doesn't break anything, and then we merge it in. :)

linux-cultist commented 6 months ago

Looks good to me, merging. Thanks for fixing this!

wit-l commented 2 months ago

This bug appears again. @linux-cultist @tianyouzeng

wit-l commented 2 months ago
  {
    "linux-cultist/venv-selector.nvim",
    dependencies = { "neovim/nvim-lspconfig", "nvim-telescope/telescope.nvim", "mfussenegger/nvim-dap-python" },
    opts = {
      -- Your options go here
      -- name = "venv",
      auto_refresh = false,
      anaconda_base_path = "D:\\Software\\anaconda3",
      anaconda_envs_path = "D:\\Software\\anaconda3\\envs",
    },
    lazy = true,
    event = "VeryLazy", -- Optional: needed only if you want to type `:VenvSelect` without a keymapping
    keys = {
      -- Keymap to open VenvSelector to pick a venv.
      { "<leader>vs", "<cmd>VenvSelect<cr>", desc = "Select Python Venv" },
      -- Keymap to retrieve the venv from a cache (the one previously used for the same project directory).
      { "<leader>vc", "<cmd>VenvSelectCached<cr>", desc = "Select Python Venv(Cached)" },
    },
  },

image image The correct path is anaconda\python.exe instead of anaconda\Scripts\python.exe on the Windows side for Anaconda.