DEV Community

Tyler Smith
Tyler Smith

Posted on

7

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.

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay