For neovim and pyenv (also works for other python version managers that uses $VIRTUAL_ENV such as virtualenvwrapper) users with the same problem on nvim (venv not working properly, causing errors on :checkhealth and !python, etc), I know this is a little late, but since neovim checkhealth redirects to this question, I'm putting this answer here hoping it will help someone.
I created a function to autosource $VIRTUAL_ENV before calling nvim. This is a little different from Tommy A answer because venv is activated on the entire nvim env, fixing !python but also letting nvim to successfully detect the pyenv venv (see :checkhealth).
For pyright problems, check PPS note at the bottom of this answer.
Here is how I did with fish:
Create file ~/.config/fish/functions/nvimvenv.fish and save the content below in it:
# Autosource virtualenv; Workaround for nvim
function nvimvenv
if test -e "$VIRTUAL_ENV"; and test -f "$VIRTUAL_ENV/bin/activate.fish"
source "$VIRTUAL_ENV/bin/activate.fish"
command nvim $argv # Run nvim program, ignore functions, builtins and aliases
deactivate # Must deactivate on exit, otherwise venv will still be sourced which may cause undesirable effects on your terminal.
else
command nvim $argv # Run nvim program, ignore functions, builtins and aliases
end
end;
There is no need to source it.
To use this function just open a new terminal and run nvimvenv, or you can set an alias to nvim by running alias nvim=nvimvenv on terminal and call it with nvim. If you want to make it permanent run with -s option like alias -s nvim=nvimvenv.
Important: If after setting the script neovim venv still not working, check if you don't have omf pyenv installed. I found that when you have omf pyenv istalled neovim can't detect pyenv virtualenv. To uninstall just run omf remove pyenv, you don't need it to use pyenv in fish, just follow the pyenv fish installation guide and your are good.
Here is the same function for bash and zsh (you will need to put/source this on your .bashrc or .zshrc):
function nvimvenv {
if [[ -e "$VIRTUAL_ENV" && -f "$VIRTUAL_ENV/bin/activate" ]]; then
source "$VIRTUAL_ENV/bin/activate"
command nvim "$@"
deactivate
else
command nvim "$@"
fi
}
alias nvim=nvimvenv
PS: Also works for vim, just replace nvim for vim in the script, but if your concern is only running the right !python Tommy answer will be enough.
PPS - Regarding pyright: Initially I erroneously mentioned that this workaround would fix pyright problems, but I found it was not true, it only fix !python and pyenv virtualenvs detection on nvim (:checkhealth). If you are facing problems with pyright you will probably have to setup a pyrightconfig.json with your venv. I created this plugin pyenv-pyright to easily setup the pyrightconfig.json with pyenv virtualenvs.