pyenv - a python equivalent to nvm and rbenv

2020-03-08

 | 

~2 min read

 | 

337 words

I recently had to run an application built with Python 3.6. Unfortunately, when I looked at the installed version of python on my OS, it was 2.7

$ python --version
Python 2.7.17

In Node-land, this is where something like nvm comes in handy for having different versions of node installed to easily move between them based on the project requirements.

(Side note: I got sick of using the wrong version to build my node projects, so I now check for a .nvmrc file every time I change directories.)

It turns out Python has a very similar tool called pyenv (and Ruby does too - rbenv).

Chris J Mendez has a great write-up on how use it to install multiple versions of python which was the main reference document I used to get up to speed.

The basic steps (using Homebrew) as Chris outlines them:

  1. Update homebrew
$ brew update
  1. Install pyenv
$ brew install pyenv
  1. Configure bash to use pyenv

If you want pyenv to run every time you boot up your terminal, you can add it to your bash_profile like so:

$ echo 'eval "$(pyenv init -)"' >> ~/.bash_profile

Alternatively, you can add it as an alias:

.bash_profile
$ alias runpyenv='eval "\$(pyenv init -)"'

To have these changes take effect, you’ll need to refresh your terminal.

$ source ~/.bash_profile
  1. Use pyenv to install the version you need

    1. List available versions

      $ pyenv install --list
    2. Install the desired version

      $ pyenv install 3.8.0
  2. Set your version of python

    1. Review which versions are installed

      $ pyenv versions
    2. locally

      $ pyenv local 3.8.0
    3. globally

      $ pyenv global 2.7.0
  3. Confirm your version is what you expect
$ python --version

You may need to reload your terminal again to see the changes take effect.

Resources


Hi there and thanks for reading! My name's Stephen. I live in Chicago with my wife, Kate, and dog, Finn. Want more? See about and get in touch!