Skip to content

Commit

Permalink
fix(pyright): use activated virtual environment if possible
Browse files Browse the repository at this point in the history
Problem:
Pyright will use system python by default, even if we already have a
virtual environment activated.

Solution:
Check current if VIRTUAL_ENV exists in environment variable, if so, set
the default pythonPath with the binary inside.
  • Loading branch information
laddertosky committed Jan 13, 2025
1 parent 8815752 commit f3d472c
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lua/lspconfig/configs/pyright.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@ local function set_python_path(path)
end
end

local python_bin = function(venv)
if vim.fn.has('win32') == 1 then
return venv .. '\\Scripts\\python.exe'
end
return venv .. '/bin/python'
end

local try_local_python_path = function()
local venv_path = os.getenv('VIRTUAL_ENV')
if venv_path then
return python_bin(venv_path)
end
return nil
end

return {
default_config = {
cmd = { 'pyright-langserver', '--stdio' },
Expand All @@ -55,6 +70,7 @@ return {
useLibraryCodeForTypes = true,
diagnosticMode = 'openFilesOnly',
},
pythonPath = try_local_python_path(),
},
},
},
Expand Down

0 comments on commit f3d472c

Please sign in to comment.