Neovim and Python

Let's face it, working with Python can be a pain at the best of times.
The second you want to work on more than one project on the same machine, you're probably going to want virtual environments to stop required packages leaking across projects.
Getting Neovim to play nicely with this kind of setup isn't as easy as installing a language server, but I've found a (relatively) straightforward solution that allows me to have properly separated environments per project, along with some globally installed packages for formatting and linting.
Requirements
We're going to need a few packages to help us get set up. First, we'll need
pyenv
and the pyenv-virtualenv
plugin for it:
1brew install pyenv pyenv-virtualenv
Virtual Environment
Next, we create a virtual environment with a recent Python version (I'll be
using 3.10.12
), which will be used for managing global packages for Neovim:
1pyenv virtualenv 3.10.12 nvim3.10
Once that is created, we'll activate the virtual environment and install some packages:
1pyenv activate nvim3.102pip install neovim black flake8
And then deactivate the virtual environment:
1deactivate
Finally, we need to tell Neovim where to find our Python installation:
1local user = os.getenv("USER")2vim.g.python_host_prog = "/Users/" .. user .. "/.pyenv/versions/nvim3.10/bin/python"3vim.g.python3_host_prog = "/Users/" .. user .. "/.pyenv/versions/nvim3.10/bin/python"
And that's it! You should now be able to use virtual environments for your
projects while still having access to black
and flake8
. All that's left
is for you to choose a language server to use, but I'll leave that up to you...