Python Version Manager

  • python
  • macos

The best option to work with Python is to use a version manager even if you are not a Python developer or you are just starting your adventure with this language. This will help you to avoid conflicts between different projects and to keep your system clean.

I want introduce to you pyenv which is very simple and easy to work with. It allows you to install multiple versions of Python and switch between them easily.

Installation

Installation is very easy, just follow the instructions on the official website but I want to show you how to install it on macOS using Homebrew.

At first, you need to install and configure Homebrew (If only you don’t have it). It is very simple. Go to brew.sh and copy the command to install.

If you have Homebrew installed, you can install pyenv with the following command:

brew update
brew install pyenv

If you have the latest macOS version (macOS Catalina, 10.15), probably you use zsh as you shell. In this case, you need to add the following lines to your .zshrc file:

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

At the end, you need to restart your terminal or run the following command:

exec "$SHELL"

# Let's make sure, `pyenv` is installed correctly
pyenv --version

Upgrading

Time to time you need to upgrade pyenv. You can do that with the following command:

brew upgrade pyenv

Usage

Very well! You have installed pyenv.

Now we can go through the basic commands.

Install python version

pyenv install 3.10.4

Set global python version

pyenv global 3.10.4

# Let's make sure, `python` is set correctly
python --version # => 3.10.4

Set local python version

pyenv local 3.10.4

Uninstall python version

pyenv uninstall 3.10.4

Other

You can list all availabile pyenv commands:

pyenv

# Detailed help about a specific command
# pyenv help <command>
pyenv help local