Python Concepts/Introduction and Setup/MacOS Installation

These instructions will help you install Python on a MacOS machine, and is part of the Introduction and Setup lesson.

Checking for an Existing Python Installation

edit

Many versions of MacOS come bundled with Python installed, but it is usually an obsolete Python 2 release. That is why you should take extra care following the steps below to determine if Python is already installed on your machine. Open the terminal app and type python --version. Compare your output to the cases outlined below:

If the command outputs "Python 3.x" (version number >= 3.0):

edit

Example:

Python 3.10.5

You already have a recent version of Python installed! Return to the lesson.

If you get "command not found" or similar:

edit

Example:

zsh: command not found: python

You don't have Python installed. Continue to Installation.

If the command outputs "Python 2.x" (version number < 3.0):

edit

Example:

Python 2.7.18

This is where things get complicated. See, you have Python 2 installed, but that's an obsolete version that is no longer supported. Try typing python3 --version instead. If that gives you a version above 3.0, return to the lesson. If not, install Python as normal below.

Warning sign Either way, keep in mind that during the course, you will always need to use the python3 command instead of the python command. This ensures you are using the correct version of Python.

Installation

edit

Installing Homebrew

edit

MacOS, unlike most Linux distributions, has no official package manager built-in. Luckily, you can install a very popular one called Homebrew, which is how you will obtain Python and keep it up to date. Run the following command to install Homebrew:[1]

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

The command will walk you through the installation process. Pay close attention to additional instructions it may provide you. Type your password if prompted for it. After you install Homebrew, restart your computer and proceed to the next section.

Installing Python

edit

After you have Homebrew installed, installing Python is only one command away! Open terminal and type:

brew install python3

This may take one or two minutes depending on your machine. After the command completes, verify the installation was successful by running python --version (or python3 --version). Congratulations, you have installed Python!

Return to Introduction and Setup

References

edit
  1. "Installing Python 3 on Mac OS X". Python Documentation. Python Software Foundation. Retrieved 2022-06-26.