Python
Installing Pyenv
I'm not sure if you have to uninstall any versions of Python before you do this, but I doubt it. You might want to checkout your bashrc to make sure there isn't any weird Python PATH stuff in there before we start.
- Install Pyenv, probably via the "Basic GitHub Checkout" method. Could also try new auto-installer, which seems to make updating easier.
- Add the necessary stuff to your
.bashrc
(not.bash_profile
) - Restart terminal
- Maybe install dependencies? (You may be good to go already.)
- Run
pyenv install --list
and pick new-ish-but-stable versions of python2 and python3 to install. For example:pyenv install 3.7.4 && pyenv install 2.7.16
- You can set version 3 as the global, with 2 as fall-back:
pyenv global 3.7.4 2.7.16
. You could also set thesystem
version as a fallback, withpyenv global 3.7.0 2.7.15 system
. - Check that
pip
is installed (I think it's included?). You should be able to upgradepip3
withpip3 install --upgrade pip
andpip2
withpip2 install --upgrade pip
Let's see how we did: If successful, python2
gets you Python 2, python3
or python
gets you Python 3. Similar with pip2
/pip3
/pip
.
To install autosub (which I think necessitates Python2) run pip2 install autosub
Deoplete / Neovim
Now that we've got a nice little Python and Pip environment set up, let's make sure Deoplete is using it.
If you use Neovim + Deoplete, you probably want to run pip3 install neovim
or pip3 install pynvim
to ensure Deoplete works smoothly (see Deoplete's requirements).
Another tip to try is to explicitedly tell Neovim which Python3 to use. Do this by including this line in your Neovim config file:
let g:python3_host_prog = expand('~/.pyenv/shims/python')
I don't think you need to provide Neovim with a version of Python2 these days to make Neovim do its job well.
At this point, you may need to update Deoplete by running :UpdateRemotePlugins
in Neovim. (Neovim's :CheckHealth
is your friend here.)
Tips for writing Python in Vim/Neovim
Formatter
For a formatter, I like Black so far. All you need to integrate it into Vim is to install the Vim plugin:
Plug 'psf/black', { 'branch': 'stable' }
And I'll probably add this to my vimrc to have it run on save:
autocmd BufWritePre *.py execute ':Black'
Note that I did not need to run any pip install commands to get it working.
Syntax and style checker
For checking Python syntax and style you could try flake8.
After installing with pip install flake8
(reference), you can run it on individual python files from the command line with flake8 <filename>.py
, which will print out recommendations to the terminal.
It also has a corresponding Vim plugin, though after installing it the default mapping, <F7>
, didn't work, so I had to run :call flake8#Flake8()
. Obviously I could try mapping it to a key myself.
2023 Update: Try ruff and these docs on Vim & Neovim integration!
2020 updates / notes
These two blog posts may offer better, newer solutions for Python environment set ups:
- https://read.acloud.guru/my-python-setup-77c57a2fc4b6
- https://jacobian.org/2019/nov/11/python-environment-2020/
pipx
Specifically, if you've been using pip to install packages solely for use on the command-line (as opposed to active development using Python), like the neovim
package, I'm hoping that pipx may be a better, more self-contained solution. Though given that you need either Homebrew or pip to install pipx, it might not be as strong of a solution on Ubuntu as it is on MacOS.
2023 updates
-
Rye looks like a possible all-in-one solution for future machines where I need access to Python.
-
There's also "Python launcher"?