- Published on
Installing and Using pyenv on macOS
- Authors

- Name
- Winston Brown
Installing and Using pyenv on macOS
If you're a Python developer, you know that managing different Python versions for various projects can be a hassle. pyenv is a fantastic tool that simplifies this process, allowing you to switch between multiple Python versions seamlessly. This guide will walk you through installing and using pyenv on your Mac.
Why use pyenv?
pyenv lets you install multiple Python versions side-by-side and choose which version to use for a specific project. This helps in avoiding dependency conflicts and ensures that your projects run on the correct Python version.
Installation with Homebrew
The easiest way to install pyenv on macOS is by using Homebrew.
Install pyenv:
brew update brew install pyenvConfigure your shell:
You need to add
pyenvto your shell's configuration file. You can copy and paste the following commands directly into your terminal.For Zsh (default on recent macOS versions):
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc echo '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc echo 'eval "$(pyenv init -)"' >> ~/.zshrcFor Bash:
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile echo '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
Restart your shell:
For the changes to take effect, restart your terminal or run:
exec "$SHELL"
Basic Usage
Here are some of the most common pyenv commands:
List available Python versions to install:
pyenv install --listInstall a specific Python version:
pyenv install 3.10.4List all installed Python versions:
pyenv versionsSet the global Python version:
This version will be used by default in your system.
pyenv global 3.10.4Set a local (project-specific) Python version:
This will create a
.python-versionfile in your current directory, andpyenvwill automatically use the specified version when you are in this directory.pyenv local 3.9.12Set a shell-specific Python version:
This version will only be used for the current shell session.
pyenv shell 3.8.13
Managing Virtual Environments with pyenv-virtualenv
While pyenv manages Python versions, the pyenv-virtualenv plugin helps you manage virtual environments for your projects.
Install pyenv-virtualenv:
brew install pyenv-virtualenvConfigure your shell for pyenv-virtualenv:
Add the following line to your shell configuration file (
.zshrcor.bash_profile):echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.zshrcRemember to restart your shell after adding this line.
Create a virtual environment:
pyenv virtualenv <python_version> <environment_name>For example:
pyenv virtualenv 3.10.4 my-project-envActivate and deactivate your environment:
pyenv activate <environment_name> pyenv deactivateYou can also set a local virtual environment for a project, which will be activated automatically when you enter the directory:
pyenv local my-project-env
With pyenv and pyenv-virtualenv, you can now manage your Python versions and virtual environments with ease. Happy coding!