From 355a552f1914e984fec5e4f6048f5179131e328d Mon Sep 17 00:00:00 2001 From: Anup Sebastian Date: Fri, 31 Oct 2025 15:46:30 -0500 Subject: [PATCH] fix: venv detection --- init.lua | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/init.lua b/init.lua index a528f548..93e3e9b7 100644 --- a/init.lua +++ b/init.lua @@ -1175,6 +1175,31 @@ vim.api.nvim_create_autocmd('FileType', { '.git' }) + -- Find Python interpreter (prioritize virtual environments) + local function find_python() + if not root_dir then + return nil + end + + -- Check common venv locations relative to project root + local venv_paths = { + root_dir .. '/.venv/bin/python', + root_dir .. '/venv/bin/python', + root_dir .. '/.env/bin/python', + root_dir .. '/env/bin/python', + } + + for _, path in ipairs(venv_paths) do + if vim.fn.executable(path) == 1 then + return path + end + end + + return nil + end + + local python_path = find_python() + vim.lsp.start({ name = 'pyright', cmd = { vim.fn.stdpath('data') .. '/mason/bin/pyright-langserver', '--stdio' }, @@ -1182,6 +1207,7 @@ vim.api.nvim_create_autocmd('FileType', { capabilities = capabilities, settings = { python = { + pythonPath = python_path, -- Tell pyright which Python to use analysis = { typeCheckingMode = 'basic', autoImportCompletions = true, @@ -1195,5 +1221,14 @@ vim.api.nvim_create_autocmd('FileType', { end, }) +-- Command to restart Python LSP (useful when switching projects/venvs) +vim.api.nvim_create_user_command('PythonRestart', function() + local clients = vim.lsp.get_clients({ name = 'pyright' }) + for _, client in ipairs(clients) do + vim.lsp.stop_client(client.id, true) + end + vim.notify('Pyright stopped. It will restart on next edit.', vim.log.levels.INFO) +end, { desc = 'Restart Python LSP (pyright)' }) + -- The line beneath this is called `modeline`. See `:help modeline` -- vim: ts=2 sts=2 sw=2 et