pyenv

Python version manager

GitHub

Table of contents
  1. What is pyenv
  2. Installation
  3. Typical usage
  4. Basic commands
    1. Check activated Python version
    2. List installed Python versions
    3. List all available Python for install
    4. Install a Python version
    5. Uninstall a Python version
    6. Show installed directory
    7. Set Python version
    8. Show Python binary
  5. Uninstall pyenv
    1. Remove all shell startup configuration
    2. Remove all Python versions
    3. Remove pyenv

What is pyenv

pyenv is a version manager for Python.

As it started off as a fork of rbenv, the syntax and usage are very similar.

It is a Python version manager not a virtual environment manager.

To manage a virtual environment for Python libraries, use in junction with venv, poetry, pipenv, etc.


Installation

Easiest way is to use Homebrew:

brew install pyenv

Then,

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(pyenv init -)"' >> ~/.zshrc

You could also add the above to .zprofile as well.

Restart shell and install Python build dependencies:

brew install openssl readline sqlite3 xz zlib tcl-tk

Typical usage

To install a specific Python version for a project, navigate to your project root and do:

pyenv intall -l  # Decide a version number
pyenv install -s 3.x.x  # -s means skip installation if it already exists
pyenv rehash  # Makes all Python binaries available to system
pyenv local 3.x.x  # Make sure you're in project root

Basic commands

Full commands are listed here

Check activated Python version

pyenv version

Do not confuse with below. Notice the plural.

List installed Python versions

pyenv versions

List all available Python for install

pyenv install -l

Install a Python version

pyenv install 3.x.x
pyenv rehash

Uninstall a Python version

pyenv uninstall 3.x.x

Show installed directory

pyenv prefix 3.x.x

Set Python version

pyenv global 3.x.x
pyenv local 3.x.x

Show Python binary

pyenv which python3

Uninstall pyenv

Remove all shell startup configuration

Remove the following from .zprofile and .zshrc:

echo 'eval "$(pyenv init --path)"'
echo 'eval "$(pyenv init -)"'

Remove all Python versions

rm -rf $(pyenv root)

Remove pyenv

brew uninstall pyenv