Pyenv can make managing multiple versions of python a lot easier. Pyenv allows you to easily select, install and compile specific versions of python for testing and development.
Setting up PyEnv
Using Brew
brew update
brew install pyenv
What is Pyenv?
Pyenv is a powerful tool that lets you easily switch between multiple versions of Python. This is particularly useful when:
- Different projects require different Python versions
- You need to test your code against multiple Python versions
- You want to try new Python features without affecting your system Python
Some key benefits of using Pyenv:
- Doesn’t require sudo access - installs everything in your home directory
- Can set Python versions per-project or globally
- Manages virtual environments (with pyenv-virtualenv plugin)
- Works seamlessly with common development tools
Prerequisites
Before installing Pyenv, you’ll need:
- A macOS system
- Command line tools installed
- A package manager (Homebrew recommended)
- Basic familiarity with terminal commands
Installation Steps
First, we’ll use Homebrew to install Pyenv. If you don’t have Homebrew installed, you can get it from brew.sh.
Install the dependencies requires to build Python. This includes the Xcode Command Line Tools and a few libraries.
xcode-select --install
brew install openssl readline sqlite3 xz zlib
Then add Pyenv to your path (In this case .zshrc):
export PYENV_ROOT="$HOME/.pyenv"
eval "$(pyenv init --path)"
eval "$(pyenv init -)"
export PATH="$PYENV_ROOT/bin:$PATH"
Installing a Python version
Install the Python version of your choice. These are installed to $(pyenv root)/versions
pyenv install <version number>
To see what versions are available:
pyenv install --list
Common Pyenv Commands
Here are some essential pyenv commands for daily use:
# List all available Python versions for installation
pyenv install --list
# Install a specific Python version
pyenv install 3.9.0
# List installed Python versions
pyenv versions
# Set global Python version
pyenv global 3.9.0
# Set local Python version (per directory)
pyenv local 3.9.0
# Set Python version for current shell session
pyenv shell 3.9.0
# Uninstall a specific Python version
pyenv uninstall 3.9.0
# Show current Python version
pyenv version
# Display full path to executable
pyenv which python
# Update pyenv itself (via homebrew)
brew upgrade pyenv
Working with Virtual Environments
If you’re using the pyenv-virtualenv plugin (recommended), here are useful commands:
# Install pyenv-virtualenv plugin
brew install pyenv-virtualenv
# Create a new virtual environment
pyenv virtualenv 3.9.0 my-virtual-env
# Activate a virtual environment
pyenv activate my-virtual-env
# Deactivate current virtual environment
pyenv deactivate
# Delete a virtual environment
pyenv virtualenv-delete my-virtual-env
# List all virtual environments
pyenv virtualenvs
Troubleshooting Tips
If you encounter issues after installation:
- Make sure your shell configuration is correct by adding these lines to your
.zshrc
or.bash_profile
:
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)" # If using virtualenv plugin
- Rehash pyenv after installing new versions:
pyenv rehash
- If build fails, ensure all dependencies are installed:
brew install openssl readline sqlite3 xz zlib tcl-tk