Skip to content

Automatically source venvs#196

Merged
theevocater merged 1 commit intomainfrom
auto_source
Feb 19, 2026
Merged

Automatically source venvs#196
theevocater merged 1 commit intomainfrom
auto_source

Conversation

@theevocater
Copy link
Copy Markdown
Owner

When in a python based project (eg, has a venv or .venv) automatically
source it before running the lsp etc.

When in a python based project (eg, has a venv or .venv) automatically
source it before running the lsp etc.
@theevocater theevocater requested a review from Copilot February 19, 2026 23:25
@theevocater theevocater merged commit 4549bba into main Feb 19, 2026
4 checks passed
@theevocater theevocater deleted the auto_source branch February 19, 2026 23:27
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds automatic Python virtualenv activation to the Neovim configuration. When Neovim starts in a directory containing a Python virtual environment (venv or .venv), it automatically activates that environment by setting the VIRTUAL_ENV and PATH environment variables. This ensures that Python LSP servers and tools use the correct virtual environment without manual activation.

Changes:

  • Added a activate_virtualenv() function that checks for venv/.venv directories in the current working directory
  • Added a VimEnter autocmd to trigger virtualenv activation when Neovim starts

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread rc.d/nvim/init.lua
end
end

vim.api.nvim_create_autocmd("VimEnter", {
Copy link

Copilot AI Feb 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The virtualenv activation only runs on VimEnter, which means if the user changes directories (e.g., with :cd or :tcd), the virtualenv won't be updated for the new directory. Consider also adding a DirChanged autocmd to re-run the activation when the working directory changes, ensuring the correct virtualenv is activated when navigating between different Python projects.

Suggested change
vim.api.nvim_create_autocmd("VimEnter", {
vim.api.nvim_create_autocmd({ "VimEnter", "DirChanged" }, {

Copilot uses AI. Check for mistakes.
Comment thread rc.d/nvim/init.lua
Comment on lines +541 to +542

vim.api.nvim_create_autocmd("VimEnter", {
Copy link

Copilot AI Feb 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The autocmd is not assigned to an augroup, which is inconsistent with other autocmds in this file (e.g., line 661 uses vim.api.nvim_create_augroup). Consider creating an augroup with clear = true to ensure the autocmd doesn't get duplicated if the config is reloaded.

Suggested change
vim.api.nvim_create_autocmd("VimEnter", {
local venv_augroup = vim.api.nvim_create_augroup("PythonVirtualenvAutoActivation", { clear = true })
vim.api.nvim_create_autocmd("VimEnter", {
group = venv_augroup,

Copilot uses AI. Check for mistakes.
Comment thread rc.d/nvim/init.lua
Comment on lines +530 to +536
for _, venv_name in ipairs(venv_paths) do
local venv_path = cwd .. "/" .. venv_name
local python_path = venv_path .. "/bin/python"

if vim.fn.isdirectory(venv_path) == 1 and vim.fn.executable(python_path) == 1 then
vim.env.VIRTUAL_ENV = venv_path
vim.env.PATH = venv_path .. "/bin:" .. vim.env.PATH
Copy link

Copilot AI Feb 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code uses Unix-specific path separators ("/bin/python"). While this may be intentional if the configuration is only used on Unix-like systems, consider using vim.fn.has('win32') to check the platform and use appropriate path separators, or use path manipulation functions that handle this automatically. This would make the configuration more portable.

Suggested change
for _, venv_name in ipairs(venv_paths) do
local venv_path = cwd .. "/" .. venv_name
local python_path = venv_path .. "/bin/python"
if vim.fn.isdirectory(venv_path) == 1 and vim.fn.executable(python_path) == 1 then
vim.env.VIRTUAL_ENV = venv_path
vim.env.PATH = venv_path .. "/bin:" .. vim.env.PATH
-- Determine platform-specific settings
local is_windows = vim.fn.has("win32") == 1 or vim.fn.has("win64") == 1
local path_sep = is_windows and "\\" or "/"
local python_dir = is_windows and "Scripts" or "bin"
local python_exe = is_windows and "python.exe" or "python"
local path_list_sep = is_windows and ";" or ":"
for _, venv_name in ipairs(venv_paths) do
local venv_path = cwd .. path_sep .. venv_name
local python_path = venv_path .. path_sep .. python_dir .. path_sep .. python_exe
if vim.fn.isdirectory(venv_path) == 1 and vim.fn.executable(python_path) == 1 then
vim.env.VIRTUAL_ENV = venv_path
vim.env.PATH = venv_path .. path_sep .. python_dir .. path_list_sep .. vim.env.PATH

Copilot uses AI. Check for mistakes.
Comment thread rc.d/nvim/init.lua

if vim.fn.isdirectory(venv_path) == 1 and vim.fn.executable(python_path) == 1 then
vim.env.VIRTUAL_ENV = venv_path
vim.env.PATH = venv_path .. "/bin:" .. vim.env.PATH
Copy link

Copilot AI Feb 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PATH modification prepends the venv bin directory without checking if it's already in PATH. If the function is called multiple times (e.g., if a DirChanged autocmd is added), this could lead to duplicate PATH entries. Consider checking if the path is already present before prepending, or removing the old venv path before adding the new one.

Copilot uses AI. Check for mistakes.
@theevocater theevocater mentioned this pull request Mar 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants