Lesson 2
The Basic Elements
Chapter 2: Installing Python
Installing Python on macOS
You can skip straight to the next page if you are not using macOS.
macOS may already come with a system Python pre-installed.
Check this by launching a Terminal (Cmd+Space, then type Terminal
and press Enter).
Then type in the following into the terminal:
$ python -V
$ python3 -V
The first command shows your system installation of Python (this might be Python 2). Don’t try to mess with this - you might break your operating system if you do not know what you are doing!
The second may be the same as the system installation (if it is not Python 2), or may be a separate installation of a Python 3.
If neither shows you a version number, or you do not have at least version 3.10, then you will need to install a new version.
There are two ways to install Python:
Download the appropriate Python installer from the official python.org website.
Run the installer to install Python on your machine.
You can also install Python 3.10 via homebrew, the de facto standard package manager on MacOS. This will allow you to easily upgrade your Python in the future, if ever needed.
First, install Homebrew with the following command in your Terminal app:
$/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
Once the above completes, run the following to install Python 3.10 on your machine:
brew install python3
Check installation
Once you have installed Python, type in the following to check that Python has been installed correctly.
$ python3.10 -V
Python 3.10.6
Homebrew may have aliased python3.10
with python3
, depending on whether a Python 3 installation existed before you installed Python 3.10 or not.
So you might also be able to run python3
instead of python3.10
as a shortcut. Just make sure this is the version you want.
The python
command may have been left pointing to an older Python 2.7 installation (may be required by System Libraries).
If you cannot run python3.10
, you should add the path to your new Python installation to your $PATH
environment variable.
##### IF YOU ARE USING BASH AS YOUR SHELL LANGUAGE
$ echo "export PATH=\"/usr/local/opt/python@3.10/bin:\$PATH\"" >> ~/.bash_profile
##### IF YOU ARE USING ZSH AS YOUR SHELL LANGUAGE
$ echo "export PATH=\"/usr/local/opt/python@3.10/bin:\$PATH\"" >> ~/.zshrc
exec "$SHELL" # restart your shell for changes to take effect