Installing PyEnv on macOS to manage your Python versions cover image

Installing PyEnv on macOS to manage your Python versions

Jonathan Barrios β€’ December 21, 2020

python data-science data-analysis

If you've been curious about managing various Python environments for Data Analysis or Data Science, this short tutorial is for you. I use Anaconda to manage my Python environments using conda, to check out that tutorial, read Create a Data Science Python Environment. In this tutorial, you'll install pyenv, and by the end of this tutorial, you will know how to:

Why PyEnv

First, a little background on why I use pyenv as part of my web development and data science workflow. I am passionate about combining web development and data science, which is why I like using pyenv with anaconda using conda, the aptly name virtual environment. Here's a simplified breakdown:

That said, there are many ways to set up a web development and data science working environment, keeping that in mind.

Install PyEnv using Homebrew

To install pyenv, make sure you have homebrew installed. To install homebrew visit their website and follow the instructions. Finally, use the following command to update homebrew:

update brew

To install pyenv, use the following command:

brew install pyenv

Install the Python version you want to use

Next, install the version of Python you want to use. You can always switch versions easily later. I installed Python version 3.9.0, which is the latest version when I published this tutorial. Use the following command to install Python using pyenv and feel free to change the Python version to suit your needs:

pyenv install 3.9.0

Set your preferred Python version global

Now that you install the Python version you want, initialize the global version of Python using the following command:

pyenv global 3.9.0

Update your configuration variables

The last part of this tutorial is to add the configuration variable for either bash or zsh. These commands will add three lines to your configuration. Here’s what each line does:

Bash

  1. The first line sets an environment variable (PYENV_ROOT) that points to the pyenv directory.
  1. The second line puts pyenv first in your search path so that the OS will find pyenv’s Python(s) before any other Pythons.
  1. The last line initializes pyenv each time you open a terminal.

Zsh

  1. The first line sets an environment variable (PYENV_ROOT) that points to the pyenv directory.
  1. The second line puts pyenv first in your search path so that the OS will find pyenv’s Python(s) before any other Pythons.
  1. The last line initializes pyenv each time you open a terminal.

Verify installation

First, you will need to close and reopen the terminal or refresh the terminal using these commands:

For bash use:

exec bash -l

For Zsh use:

exec zsh -l

Next, confirm your installation using the following command: pyenv version

Finally, verify which version of Python you have: python -V

PyEnv and Anaconda/Conda conflict

I ran into a conflict using pyenv and anaconda while using the conda virtual environment. In short, conda wasn't using the specified python version I delclared while creating the virtual environment. To fix this I added the following to the ~/.zshrc file and if you are using bash add it to the ~/.bash_profile or ~/.bashrc file:

# Add this code to ~/.bashrc or ~/.bash_profile for Bash,
# .zshrc for ZSH

# you may need to add dir of command `pyenv` into PATH,
# if command pyenv is not available yet

if command -v pyenv &>/dev/null; then
    eval "$(pyenv init -)"
fi
if command -v pyenv-virtualenv &>/dev/null; then
    eval "$(pyenv virtualenv-init -)"
fi

Finally, I also need to fix a brew doctor warining by using the following alias:

alias brew='env PATH="${PATH//$(pyenv root)\/shims:/}" brew'

This is how I got both Pyenv and Anaconda/Conda to play nicely together. πŸ€“

Verify installation once more

Refresh the terminal using these commands:

For bash use:

exec bash -l

For Zsh use:

exec zsh -l

Next, confirm your installation using the following command: pyenv version

Verify which version of Python you have: python -V

Finally, run brew doctor to make sure you are getting the Warning: "config" scripts exist outside your system or Homebrew directories. warning with the following code:

brew doctor

Congratulations! πŸŽ‰

If everything went according to plan and you see the correct version of python, congratulations! If not, send me a DM on Twitter. As always, happy coding! 🐍