Manage Multiple Python Versions with pyenv
Install and switch between multiple Python versions on the same machine using pyenv, with per-project version pinning.
Prerequisites
- -macOS, Linux, or WSL (pyenv does not run natively on Windows)
Steps
Install pyenv
Install pyenv and its plugins (pyenv-virtualenv, pyenv-update). The installer adds pyenv to your home directory at ~/.pyenv.
After installation, you must add pyenv to your shell PATH. Follow the instructions printed by the installer to update your .bashrc or .zshrc.
List available Python versions
Show all available CPython 3.x versions that can be installed. The list includes stable releases and pre-release versions.
Look for the latest stable version (e.g., 3.12.x or 3.13.x) without a, b, or rc suffixes.
Install a specific Python version
Download and compile the specified Python version. It is installed to ~/.pyenv/versions/ and does not affect your system Python.
Set the global default Python version
Set the default Python version used in all shells. This writes to ~/.pyenv/version.
Set a per-project Python version
Pin a specific Python version for the current directory by creating a .python-version file. pyenv automatically switches versions when you enter this directory.
Commit .python-version to your repository so all team members use the same Python version.
Verify the active Python version
Display which Python version pyenv has selected and confirm that the python command resolves to it.
Full Script
FAQ
Discussion
Loading comments...