When using Python, you may install different version variations for different projects. But sometimes this can affect how your code executes, as it may not use the correct version.

In this article, we'll learn how to install new Python versions (Python 3 in our case) and how to set this version as the active version for code execution.

Install Pyenv

GitHub - pyenv/pyenv: Simple Python version management
Simple Python version management. Contribute to pyenv/pyenv development by creating an account on GitHub.
pyenv

If you're familiar with NodeJS, you'll know that nvm is used for managing versions of Node in different environments. pyenv does the same thing for Python – it's a version management tool.

This tool helps you to work on different environments which require different versions of Python.

Install pyenv using Homebrew with the following command:

Here's the command to install Python 3 on Mac:

brew install pyenv

Make sure you follow the rest of the steps for installing pyenv in the documentation.

ADVERTISEMENT

Install Python 3

With pyenv installed, you don't need to install Python with Homebrew anymore (as you may already be doing). You can install Python using pyenv with the following syntax:

pyenv install [version]

The version argument follows semantic versioning which is "major.minor.patch".

For Python 3, let's say we want to install 3.10.2. Then we'll use this command:

pyenv install 3.10.2

To see the list of the Python versions we have, we use the following command:

pyenv versions

In my case, I have:

image-6
Currently installed python versions on my system

From the screenshot above, the asterisk shows the currently active Python version, which is the default system version:

python --version
# Python 2.7.18

To set the newly installed version as the default, here's how to do it (among many other ways):

pyenv global 3.10.2

python --version
# Python 3.10.2

If your python version remains the same, you have to make sure that you add the required init command as you can see in the documentation: Basic GitHub Checkout – 2. Configure your shell's environment for Pyenv

With all that in place, you can now use Python 3.

ADVERTISEMENT

Update Python Version

With more versions being released, you may want to update your version. You can update your version by installing a new version, making it your global default, and optionally uninstalling the old version.

Here are the commands for that:

pyenv install new.python.version

pyenv global new.python.version

pyenv uninstall old.python.version

Thank you for reading! I hope you now have the version of Python installed that's most useful to you.