Making pyenv and neovim play nice (and a few gotchas found along the way)
For my development workflow and IDE, I’ve recently decided to move from Vim, to NeoVim, which is essentially Vim on steroids.
No, but seriously, NeoVim has a few features that are really interesting, but the real reason for moving is that, as a Python dev, I need to be able to look at source code and definitions very often, while hacking on my code. The wonderful jedi-vim plugin normally allows me to do just that, when it comes to my local project. But because I am using virtual environments extensively in my work, such as with virtualenv and/or pyenv, I have a problem on my hands:
Jedi-vim is painfully unaware of virtual environments.
So if I for example,
setup a virtual environments in my project dir as such:
|
|
Activate the environment:
|
|
and then install Django:
|
|
Initialise a django app with:
|
|
Open the urls file:
|
|
Placing the cursor on an object we are importing,
such as url, and running the jedi-vim goto definition shortcut (mine is ,d
):
|
|
Returns the following error:
|
|
Which sucks.
Spent a good amount of time googling, asking around, playing with different plugins, but nope, no joy. The internet was suggesting three things:
- Suck it up, cause similar issues were not tackled yet or at all..
- Tackle the issue myself, for which I feel I am not well versed enough (yet).
- Move to NeoVim plus Pyenv and jedi-vim, where things magically work.
So here I am with my brand new NeoVim setup. Of course I had to go through a few gotchas to get this to work, which I will share here for all those out there hacking around with this.
Installing Pyenv
The first thing I did was to ditch virtualenv and use Pyenv instead. I used homebrew to do that.
|
|
We still need to complete the install by adding a few environment variables to our shell profiles. So depending on our preferred shell, we use ~/.bash_profile , or ~/.zshrc or whatever else:
|
|
(more info on PyEnv Github)
We can then initialize some environments and play around:
|
|
Installing NeoVim and jedi-vim
For NeoVim, I used, you guess it, Homebrew:
|
|
At which point, we can run it with:
|
|
I then proceeded to install a bunch of plugins for my Python stuff. I took a great deal of inspiration from Fisa’s NeoVim configuration for that, but for the sake of this guide, we can simply install the jedi-vim plugin by adding the following to our ~/.config/nvim/init.vim :
|
|
We can then reopen nvim
and run a :PlugInstall
Now for our gotcha part
For being able to use Python 2/3 plugins,we will need the neovim Python module, with:
|
|
But this would mean that we’d have to install the neovim plugin in all the virtual environments we create.
To get around this, we need to create 1-2 virtual environments just for neovim, and add a line in our init.vim
that will tell nvim to look into those envs for its plugin. We do the following:
|
|
We will need to add also the following to our ~/.config/nvim/init.vim:
|
|
Run a health check in nvim to make sure all is ok:
|
|
Once this is done, we are all set with a reachable pyenv environment in our shiny new neovim setup!