pyenv

pyenv is a command line tool that lets you switch between multiple versions of Python.

For example, it allows you to create one project with Python 3.13, and another with 3.12.

Install

Install pyenv

Install pyenv with Homebrew.

brew install pyenv
Tip

If you have trouble installing, consider installing these pre-requisites.

xcode-select --install
brew update && brew install openssl readline sqlite3 xz zlib tcl-tk

Install Python versions

List Python choices.

pyenv install -l

Filter the list. For example, list all available Python 3.13.x versions:

pyenv install -l | grep 3.13

Install a Python version, like python_version="3.13.2".

pyenv install ${python_version}

Install other versions. You can now switch between them.

Run

You can also unset the local Python version.

pyenv local --unset
Tip

If pyenv local is not working, or .python-version is not respected, run this:

pyenv shell --unset

Switch back to the system Python.

pyenv global system

Troubleshooting

If an existing Python version installation gives you trouble, uninstall and reinstall. If that doesn't work, uninstall the version, install the pre-requisites below.

brew install openssl readline zlib xz

You may need to add these to your .zshrc file.

export LDFLAGS="-L$(brew --prefix openssl)/lib -L$(brew --prefix readline)/lib -L$(brew --prefix zlib)/lib"
export CPPFLAGS="-I$(brew --prefix openssl)/include -I$(brew --prefix readline)/include -I$(brew --prefix zlib)/include"
export PKG_CONFIG_PATH="$(brew --prefix openssl)/lib/pkgconfig:$(brew --prefix readline)/lib/pkgconfig:$(brew --prefix zlib)/lib/pkgconfig"

References

pyenv
Interactive graph
On this page
Install
Install pyenv
Install Python versions
Run
Troubleshooting
References