Using pyright with pyenv and its .python-local feature #4420
Replies: 3 comments
-
Since few days ago I must do |
Beta Was this translation helpful? Give feedback.
-
Do you have any solution to My
Withtout this, I keep getting a bunch of eg: Any other options so that this could be comitted to github and every user with different path could use it? |
Beta Was this translation helpful? Give feedback.
-
does using shell environment variables work for you? 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 vim=nvimvenv
alias nvim=nvimvenv |
Beta Was this translation helpful? Give feedback.
-
This is more of a pyenv specific continuation of #4021 and knowledge dump than anything else.
tldr
As per #4021, pyright doesn't and won't execute
python
in the current working directory and this breaks tools such as pyenv which can depend on the current working directory to dispatch to the correct virtualenv.Getting pyright and pyenv to play along
So far the workarounds I see are:
PYENV_VERSION
environmental variable to the name of your virtualenv before starting your editor. You can usepyenv shell my_virtual_env
ordirenv
to automate it or any other method you can think of. This might still require you to set pyright's globalpython.pythonPath
to/home/YOUR_USER_NAME/.pyenv/shims/python
if your environment'sPATH
is not setup correctly. Else the default value ofpython.pythonPath
(which ispython
afaik) should suffice with a default pyenv installation.pyrightconfig.json
(or equivalent for your editor) in your project directory and setup eitherpython.pythonPath
or some combination ofpython.venvPath
andpython.venv
so that the actual location of the virtualenv is known by pyright. You can use https://github.com/alefpereira/pyenv-pyright to generate this for you whenever you need it.All of them require some kind of per project configuration on top of the existing pyenv configuration, which isn't great.
Warning
Any of pyenv's shims configured in pyright's
python.pythonPath
will ONLY work if you have thePYENV_VERSION
environmental variable set. Using values such as/home/USER/.pyenv/versions/VIRTUAL_ENV/bin/python
also don't work as they will (likely) resolve to one of pyenv's shims.Longer explanation
pyenv
has 3 main methods for locating the virtualenv the user is expecting (https://github.com/pyenv/pyenv#understanding-python-version-selection):PYENV_VERSION
environmental variable, as set bypyenv shell my_virtual_env
.python-version
files, as created bypyenv local my_virtual_env
pyenv global my_virtual_env
.pyright's logic to determine the current virtualenv depends on executing the currently available
python
located via the environment and from that determine the active virtualenv by interrogatingsys.path
. This works when the current environment directs pyright to the active virtualenv's python (method 1) as the executing environment variables (PYENV_VERSION
) are preserved.Method 2 however is a more user friendly way of configuring virtualenvs for projects and this depends on the current working directory being where the user executed it from (directly or indirectly via an editor using LSP and pyright).
Related discussions
PYENV_VERSION
from.python-version
could not be resolved if use pyenv local fannheyward/coc-pyright#184 (comment) and fannheyward/coc-pyright@77e02e0Beta Was this translation helpful? Give feedback.
All reactions