DEV Community

Tyler Smith
Tyler Smith

Posted on

Using a venv with Neovim's Python LSP

I recently started coding with Neovim using kickstart.nvim as the template for my editor configuration. I downloaded the python-lsp-server package using Mason, but I was disappointed to discover that the IntelliSense on my third party dependencies didn't work. The LSP was resolving to my global Python installation, which did not have the packages from my virtual environment (venv) installed.

In VS Code, I can press cmd + shift + P and then type Python: Select Interpreter, which allows me to select the interpreter from my project's virtual environment. However, I did not see an equivalent feature available in my Neovim installation. A cursory Google search for "use venv on neovim" turned up dozens of results about Neovim plugins that can be used to manage virtual environments automatically. Because I was brand new to Neovim, I didn't yet feel comfortable installing and configuring plugins in Lua.

A simpler way

Buried in one of the search results where users offered suggestions for venv management plugins, a user named vihu offered a much simpler solution that did not require additional software or configuration.

Here is the gist of vihu's solution:

cd my_python_project
source .venv/bin/activate
nvim
Enter fullscreen mode Exit fullscreen mode

That's it.

By sourcing the venv before you start Neovim, the python-lsp-server automatically uses the venv Python interpreter and its dependencies. This provides IntelliSense for third party modules installed inside the virtual environment.

I hope this helps you as we suffer through Neovim together.

Top comments (0)