Skip to content

Commit 355a552

Browse files
Anup SebastianAnup Sebastian
authored andcommitted
fix: venv detection
1 parent a6ff6da commit 355a552

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

init.lua

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,13 +1175,39 @@ vim.api.nvim_create_autocmd('FileType', {
11751175
'.git'
11761176
})
11771177

1178+
-- Find Python interpreter (prioritize virtual environments)
1179+
local function find_python()
1180+
if not root_dir then
1181+
return nil
1182+
end
1183+
1184+
-- Check common venv locations relative to project root
1185+
local venv_paths = {
1186+
root_dir .. '/.venv/bin/python',
1187+
root_dir .. '/venv/bin/python',
1188+
root_dir .. '/.env/bin/python',
1189+
root_dir .. '/env/bin/python',
1190+
}
1191+
1192+
for _, path in ipairs(venv_paths) do
1193+
if vim.fn.executable(path) == 1 then
1194+
return path
1195+
end
1196+
end
1197+
1198+
return nil
1199+
end
1200+
1201+
local python_path = find_python()
1202+
11781203
vim.lsp.start({
11791204
name = 'pyright',
11801205
cmd = { vim.fn.stdpath('data') .. '/mason/bin/pyright-langserver', '--stdio' },
11811206
root_dir = root_dir or vim.fn.getcwd(),
11821207
capabilities = capabilities,
11831208
settings = {
11841209
python = {
1210+
pythonPath = python_path, -- Tell pyright which Python to use
11851211
analysis = {
11861212
typeCheckingMode = 'basic',
11871213
autoImportCompletions = true,
@@ -1195,5 +1221,14 @@ vim.api.nvim_create_autocmd('FileType', {
11951221
end,
11961222
})
11971223

1224+
-- Command to restart Python LSP (useful when switching projects/venvs)
1225+
vim.api.nvim_create_user_command('PythonRestart', function()
1226+
local clients = vim.lsp.get_clients({ name = 'pyright' })
1227+
for _, client in ipairs(clients) do
1228+
vim.lsp.stop_client(client.id, true)
1229+
end
1230+
vim.notify('Pyright stopped. It will restart on next edit.', vim.log.levels.INFO)
1231+
end, { desc = 'Restart Python LSP (pyright)' })
1232+
11981233
-- The line beneath this is called `modeline`. See `:help modeline`
11991234
-- vim: ts=2 sts=2 sw=2 et

0 commit comments

Comments
 (0)