🏞️ pyenv

Updated at 2019-01-26 22:09

pyenv is a Python version management that make it easier to run multiple versions of Python on the same machine. It should be only used for development; not for production.

How to setup pyenv on Mac with some optional parts if you have pre-existing Python:

# uninstall all previous versions of Python
brew list | grep python
brew uninstall --ignore-dependencies python@2
brew uninstall --ignore-dependencies python3
brew uninstall --ignore-dependencies python

# the next command should return nothing
brew list | grep python

# python executable should be the Apple default
# Python 2.7.10 (default, Feb  7 2017, 00:08:15)
# [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin

# uninstall everything that relied on those Python versions
brew uninstall ansible asciinema aws-sam-cli awscli awslogs

brew update
brew install pyenv


# shows which Python versions are installed
pyenv versions

# shows which Python version is active
pyenv version

# shows which Python versions are available to download
pyenv install -l

# install what you want...
pyenv install -l | grep 2.7.
pyenv install 2.7.15
pyenv install -l | grep 3.6.
pyenv install 3.6.8
pyenv install -l | grep 3.7.
pyenv install 3.7.2

# enable shell integration, follow the insturctions
pyenv init

# define default Python
pyenv global 3.7.2

pyenv versions
#   system
#   2.7.15
#   3.6.8
# * 3.7.2 (set by $HOME/.pyenv/version)

python --version    # Python 3.7.2
which python        # $HOME/.pyenv/shims/python
pyenv which python  # $HOME/.pyenv/versions/3.6.8/bin/python

pip install virtualenv
pip list                # pip, setuptools, virtualenv

# you can define what Python version are for each project (directory), even multiple
pyenv local 3.5.1 2.7.10
python3.5 --version
python2.7 --version

Bash Usage:

# enable Python with some virtualenv installed
export PYENV_VERSION=`pyenv whence virtualenv | tail -1`

# take latest Python that matches the target version
export PYENV_VERSION=`pyenv whence $PYTHON_EXE | tail -1`