Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Installing pyenv on macOS for Zsh using Homebrew

Installing pyenv on macOS for Zsh using Homebrew

Published May-13-2020


Install pyenv and pyenv-virtualenv thru Homebrew

~$ brew update
~$ brew install pyenv
~$ brew install pyenv-virtualenv

Add the following lines to the end of the .zshrc file in your root directory (~)

export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
export PIPENV_PYTHON="$PYENV_ROOT/shims/python"


eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

Reload the .zshrc file to apply the changes made

~$ source ~/.zshrc

See all available versions for pyenv

~$ pyenv install --list
Available versions:

Install a Python version from the list above. Note that this may take some time to complete

~$ pyenv install 3.5.6

Check on the installed pyenv Python versions

~$ pyenv versions
* system (set by /User/user/.pyenv/shims/version)

Basic Usage

Switch the global Python to a specific version

~$ pyenv global 3.5.6
~$ python -V
Python 3.5.6

Switch back the global Python to the system version

~$ pyenv global system
~$ python -V
Python 3.7.7

You can also set a project-specific version of Python

~$ pyenv global system
~$ mkdir project
~$ cd project
project$ pyenv local 3.5.6
project$ python -V
Python 3.5.6
project$ cd ..
~$ python -V
Python 3.7.7


Create a virtualenv for the Python version used for pyenv

~$ pyenv virtualenv 3.5.6 venv35
~$ pyenv activate venv35
(venv35) ~$ pyenv deactivate

Check on the installed pyenv virtualenvs

~$ pyenv virtualenvs
venv35 (created from /Users/mari/.pyenv/shims/versions/3.5.6)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment