pyenv is a tool which can help you manage the multiple Python versions on your system. pyenv-virtualenv is pyenv a plugin which manages virtual envs.

Install

Install with brew on macOS.

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

Add the following two lines to the profile of your shell:

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

Start a new shell, and you can use them.

Usage of pyenv

# list all the Python versions supported by pyenv
$ pyenv install -l 

# add Python 3.6.2 to your system
$ pyenv install 3.6.2

# show all the Python versions installed
$ pyenv versions

# create a directory for test
$ mkdir pyenv-test
$ cd pyenv-test

# set the Python version used for this project
# .python-version will be created under current dir
$ pyenv local 3.6.2

# show your current Python version
$ pyenv version
3.6.2

# double check the version
$ python -V
Python 3.6.2

# check the path of an executable
$ pyenv which python
$ pyenv which pip

Usage of pyenv-virtualenv

# create a virtualenv named venv
$ pyenv virtualenv venv

# check the versions again
# venv is a symblic link to 3.6.2/envs/venv under ~/.pyenv/versions
$ pyenv versions
...
3.6.2
3.6.2/envs/venv
venv

# check the path of an executable
# the path will be under venv now
$ pyenv which python
$ pyenv which pip

# list all the virutalenvs
$ pyenv virtualenvs

$ pyenv deactivate venv

# uninstall above venv
$ pyenv uninstall venv

References

  1. https://github.com/pyenv/pyenv
  2. https://github.com/pyenv/pyenv-virtualenv