A Detailed Guide: Updating Python on Mac

A laptop awaiting an upgrade, placed on a table with a cup beside it

Enhancing the condition of your Python ecosystem is a fundamental aspect of becoming a skilled programmer. By ensuring your installation is always up to date, you can steer clear of potential risks associated with obsolete features and security patches. Immerse yourself in this comprehensive guide specifically tailored for Mac users, aiming to demystify the Python updating process. By diligently following the meticulously outlined instructions, you can anticipate a seamless and remarkably efficient procedure.

Python and MacOS: The Initial Scenario

While Python comes pre-installed with macOS, the default version that is typically used is outdated. For instance, macOS Catalina (macOS 10.15) introduced Python 2.7 as the default distribution. However, Python 2 is no longer supported and will not receive any updates, security patches, or improvements after January 1, 2020. To stay at the forefront of the Python ecosystem, it is crucial to upgrade your Python installation to Python 3.

Before delving into the main method, it is important to understand Python’s significance within macOS. Therefore, we strongly advise against installing a different Python version than the one that came with your computer. Instead, we will guide you through the process of installing the new Python version parallel to the old one. Let’s explore the detailed breakdown of the procedure.

Homebrew: The Foundation Step

Installing a reliable package manager on your Mac is a prerequisite to continuing with Python updates. Because of this, managing software installations will be a breeze. The installation process is greatly simplified by using Homebrew, a wonderful free and open-source package management system made especially for macOS.

Installing Homebrew

Let’s have a look at how to set up Homebrew:

  • Launch Terminal: To access the Terminal app on your Mac, go to the Applications folder, then the Utilities folder.
  • Enter the Homebrew installation command: When Terminal is ready, paste this command into it from here:
/bin/bash -c “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)”
  • Run the command: You may make the command take effect by pressing the Enter key on your keyboard.

Your Mac will soon be updated with Homebrew. Since Homebrew requires downloading some files during installation, the time it takes to install will vary depending on the speed of your internet connection.

Updating Python with Homebrew

he homepage of Homebrew, featuring the installation link

Now that Homebrew is set up, the most recent version of Python may be installed with it.

Install Python 3 with Homebrew

  • Open Terminal: The same path as before will lead you there: Applications > Utilities > Terminal.
  • Update Homebrew: Before moving forward, we must check that Homebrew is up to date. In Terminal, enter this command and hit Enter:
brew update
  • Install Python 3: Now, in Terminal, type the following command to set up Python 3:
brew install python3
  • Confirm Python installation: After the installation is complete, enter this command to see what version of Python is present on your Mac:
python3 –version

The Era of Multiple Python Versions: Understanding Pyenv

Sometimes developers have to switch back and forth between Python versions. For this reason, having a Python version manager is invaluable. Pyenv, a simple yet effective and cross-platform tool for managing several Python versions, is a favourite among developers.

Installing Pyenv

To set up and begin utilizing Pyenv, do as follows:

  • Open Terminal: The same path to Terminal (Applications > Utilities > Terminal) should be used as before.
  • Install Pyenv: To set up Pyenv with Homebrew, enter the following command:
brew install pyenv
  • Configure Pyenv: Once Pyenv is set up, you can add it to your shell so that it launches automatically whenever you do. These lines should be included in your shell’s config script. For bash users, the file you need to add these lines to will be either ~/.bash_profile, ~/.bashrc, ~/.zshrc, ~/.zprofile, or ~/.zlogin. We’ll use ~/.bash_profile as an example:

Open the file using nano or your preferred text editor:

nano ~/.bash_profile

And then add these lines at the end of the file:

if command -v pyenv 1>/dev/null 2>&1; then
  eval “$(pyenv init -)”
fi

To permanently commit your edits in the nano editor, press Ctrl+O followed by Enter. To close the editor, hit Ctrl+X.

  • Restart your shell: To let the new settings take effect, you must now restart your shell. To do so, either type the following command into your terminal or close and reopen it.
exec “$SHELL”

Using Pyenv to Manage Python Versions

Now that you have Pyenv, you can set up multiple Python environments and easily switch between them as needed. How to achieve this is as follows:

  • Install a Python version: Installing Python 3.8.1 is one example. To proceed, please type:
pyenv install 3.9.1
  • Switch to a specific Python version: Enter this to set Python 3.9.1 as the system-wide default:
pyenv global 3.9.1
  • Confirm the version change: Confirm that the switch was successful with:
python –version

The pyenv install –list command also displays all upgradeable versions. You can use pyenv global system to revert to the Python version that came with your operating system.

Uninstalling Python Versions in Pyenv

At some point, you may find that you need to uninstall a Python version that you installed via Pyenv. In this way:

  • Open Terminal: Navigate to Terminal via Applications -> Utilities -> Terminal.
  • Run the uninstall command: Type in the following command to uninstall a specific Python version. Replace ‘version’ with the version number you wish to uninstall:
pyenv uninstall version

Conclusion

Acquire comprehensive knowledge and expertise in managing and optimizing Python on your Mac through this in-depth tutorial. Unleash the full capabilities of Homebrew and Pyenv to effortlessly handle a versatile Python development environment. Familiarize yourself with the advantages offered by the latest Python versions and effortlessly switch between different versions as required. Embark on a journey into uncharted territory and uncover the path to mastering Python.

FAQ

How do I check the current version of Python on my Mac?

To determine which version of Python you are using, open the Terminal and type python –version or python3 –version.

I am having problems during the Python installation. What can I do?

To begin, type brew update to update Homebrew. If the problem persists, you can utilize brew doctor to identify and fix widespread issues.

How do I ensure my Python scripts run with the latest version of Python?

lways remember to shebang off properly when writing scripts. Use #!/usr/bin/env python3 for scripts written in Python 3.

How can I keep track of all Python versions on my system?

The pyenv versions command in Pyenv will display a list of all Python distributions currently set up on your machine.

What happens when I run pyenv global system?

The pyenv global system command restores the system-wide Python installation as the default.

Recommended Articles