19

I am using the pyright LSP in neovim (0.5). It works, but seems to only pick up on packages available in the standard python installation. It does not autocomplete for packages not in the base python, but in my pyenv environment. In VSCode this is quite easily done by selecting the interpreter.

How can I set the virtual environment or pyenv version to be used by Pyright LSP in Neovim?

5 Answers 5

27

This is not actually an answer, there is this issue in nvim-lsp where they explore some alternatives, it turns out nvim (or pyright, I don't know exactly) don't respect/load pyenv local .python-version file. An alternative is to use regular venv. Using pyenv shell myvenv before running nvim also works, but it goes against the convenience of .python-version file. Maybe there is a way to load the correct venv with some scripts in bashrc/zshrc/config.fish, but again this is not that convenient, IMHO.

https://github.com/neovim/nvim-lspconfig/issues/717

EDIT: Found a good solution

There is a simple way to get pyright work with pyenv virtualenvs:

Create pyrightconfig.json file in root directory of your project, and paste the following, relacing USERNAME and MY-VENV with your user and venv, supposing your pyenv is installed in ~/.pyenv. It adds another file beyond .python-version, but its easy and don't mess with your shell configs.

{
    "venvPath": "/home/USERNAME/.pyenv/versions/",
    "venv": "MY-VENV"
}

You can check full doc here: https://github.com/microsoft/pyright/blob/master/docs/configuration.md

EDIT 2: Checkout this plugin pyenv-pyright I created. With it you can setup pyright to use pyenv venvs with only one command:

pyenv local my-venv
pyenv pyright

or

pyenv pyright my-venv

This will automatically create/update pyrightconfig.json file with the pyenv virtualenv of your choice. Its a convenient way to overcome neovim+pyright+pyenv virtualenvs setup. https://github.com/alefpereira/pyenv-pyright

Sign up to request clarification or add additional context in comments.

3 Comments

Using pyrightconfig.json file works for me too. However, I am using a local .venv in the project root so I had to set venvPath to '.' and venv to '.venv' for it to work
This works! Also (recommended from this thread) you should set exclude = ["./.venv"]. Note you can also put pyright settings in a pyproject.toml under the tool.pyright section!
Doesn't work for me: Neovim 0.10.x (pyright-langserver 1.1.391)
9

TLDR;

echo '{ "venvPath": ".", "venv": ".venv" }' >> pyrightconfig.json

It works with the default pyenv and poetry setup.

Comments

3

I failed to make other options work, so I use

pyenv shell [virtual_env_name]

and it works for me.

Comments

3

I use LazyVim as nvim distribution, and in their setup they include venv-selector plugin. It has worked quite well to me out of the box.

3 Comments

I’m also using lazy vim but I’m a beginner, how can i set this up? Is there a guide besides the GitHub repo?
Hi Gabriel, in the lazyvim docs you can find the vanilla setup which worked for me
venv-selector doesn't seem to be listed in LazyExtras any more because of this issue: github.com/LazyVim/LazyVim/discussions/5081
1

seems like this config is working for me in nvim to activate either a virtual env, a pyenv env, or fallback to the standard python installation

in my init.lua

local function get_python_path()
    -- Use activated virtualenv
    if vim.env.VIRTUAL_ENV then
        return vim.env.VIRTUAL_ENV .. "/bin/python"
    end

    -- Find and use pyenv environment
    local pyenv_path = vim.fn.system("pyenv which python"):gsub("\n", "")
    if vim.fn.filereadable(pyenv_path) == 1 then
        return pyenv_path
    end

    -- Default to system Python
    return vim.fn.exepath("python3") or vim.fn.exepath("python") or "python"
end

require('lspconfig').pyright.setup({
    before_init = function(_, config)
        config.settings.python.pythonPath = get_python_path()
    end
})

1 Comment

Thank you for your interest in contributing to the Stack Overflow community. This question already has existing answers. It would be useful to explain how your approach is different, under what circumstances your approach might be preferred, and/or why you think the previous answers aren’t sufficient. Can you kindly edit your answer to offer an explanation?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.