Pyenv Quick Reference

Contents

Pyenv is a tool for easily switching between multiple versions of Python. One way to use the tool is in conjunction with the pyenv-virtualenv and pyenv-virtualenvwrapper plugins.

Detailed instructions are available here and here. The following reference is a brief summary of some frequently used commands.

Install Pyenv, pyenv-virtualenv and pyenv-virtualenvwrapper

Pyenv Installer

First use the Pyenv Installer. The ‘Github Way’ of installing is easiest.

Edit your profile (i.e. ~/.bash_profile, ~/.zshenv, etc) and add the following, adjusting paths as necessary:

# Use pyenv for Python version management
export PYENV_ROOT="$HOME/Dev/.pyenv" <--Use your chosen location here
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"

Then install the following two plugins:
pyenv-virtualenv
pyenv-virtualenvwrapper

Edit your profile again, adding the following:

eval "$(pyenv virtualenv-init -)"
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/Dev
source /usr/local/bin/virtualenvwrapper.sh
pyenv virtualenvwrapper

Usage

List Available Python Versions

pyenv install -l

Install a specific version

pyenv install 3.6.4

Create A New virtualenv Based on a Specific Python Version

See the full guide here.

pyenv versions
pyenv shell 3.6.4
mkvirtualenv --python=`pyenv which python` mynewenv

Customize pip.conf

You can set a project or virtualenv specific pip.conf by creating one in
$VIRTUAL_ENV/pip.conf when the virtualenv is active:

$ workon myenv
$ vi $VIRTUAL_ENV/pip.conf

Add this:

[global]
index-url = https://<path_to_a_corporate_repo>

Other Useful Commands

CommandDescription
lsvirtualenvList all the virtualenvs
echo $WORKON_HOMEShow the directory where virtualenvs are located
workon myvirtualenvSet the current virtualenv to myvirtualenv
pyenv globalShow the global Python version currently in use
pyenv versionsShow which Python versions are installed
pyenv global 3.6.4Switch to the 3.6.4 version if installed