Introduction
Python is a flexible programming language that gives you a lot of power in MacOS. We'll look at four methods of setting up Python on MacOS.
- Using pre-packaged system Python
- Using the official Python.org installer
- Using Homebrew to install
- Compiling Python from source
Python installation methods
Let's review some of the different options for installing Python on MacOS.
Use system Python (Not recommended)
The latest version of MacOS comes with Python 3 but there are a number of reasons I recommend not using it:
- It will be older version than latest version
- It will not come with the Tk gui package
- It will not have the IDLE editor
- The system uses the packages, so you will have to be careful what you install and use sudo, or do user-only package installs for pip, or create a venv - nothing wrong with creating a venv and adding it to your
.zshrc
to use it all the time
Official Python.org installer (Recommended for everyone)
The official Python installer is the easiest and also provides the latest version with all of the goodies like IDLE and Tkinter. To get the installer, go to Python.org and download the version for your system. You can also find the documentation there.
- Will come with documentation, IDLE editor, and Python Launcher applications to your app menu
- The Python launcher can be used to execute Python (
.py
/.pyw
) scripts from the Finder - Installs to a path similar to
/Library/Frameworks/Python.framework/Versions/3.8/bin/python3
- You can run it using
/usr/local/bin/python3
If you need to set up your PATH
, you can add a line like this to your .zshrc
:
export PATH=/Library/Frameworks/Python.framework/Versions/3.8/bin:$PATH
Using Homebrew (Recommended for advanced)
You can use the Homebrew package manager to install Python. Install with the following command:
brew install python3
This installs to: /usr/local/Cellar/python
which the interpreter symlinked at /usr/local/bin/python3
and idle symlinked at /usr/local/bin/idle3
.
Compiling Python from source code (Recommended for advanced)
Compiling Python from source is a lot more work but can get you the latest development version of Python. You will have to take many additional things in to consideration like which extensions to include and what options to set. It can be a good exercise in learning. I have a dedicated tutorial about Building Python from Source.
Testing install
You can run a few commands to help identify the installed Python versions and which one
will be run by default when only running python3
without the full absolute path.
# See all `python3` executables available
where python3
# See the default/primary `python3` on the `PATH`
which python3
# See what version of Python is run
python3 --version
Notes about multiple installations
It is possible to have multiple versions of Python installed. In addition to the system Python 2 and Python 3, you can also have Python 3 installed from the official Python.org installer as well as Homebrew's Python 3 all at once. It can get confusing and you may accidently install a package to one version of Python while your IDE is using a different version.
They can all co-exist if you know what you are doing, but I recommend choosing either the Homebrew method or the official installer and not both. I prefer the official installer.
You can use where python3
or where idle3
to see which executables you have available in your path.
# Where which executables are available
where python3
where idle3
The /usr/bin/python3
is the system Python, and the one you want to avoid using. Homebrew and the official Python installer will both symlink executables to /usr/local/bin/python3
depending on which one was installed last.
The system Python (in /usr/bin/
) may end up in your PATH
environment variable before your installed version (in /usr/local/bin/
). This is one reason I recommend creating a virtual environment and explicitly activate it (see the next section on virtual environment).
If you want to modify your path so one Python has precedence over the other, you can update your PATH
in your .zshrc
/.bashrc
file to put the preferred directory before the rest of your path:
export PATH=/path/to/preferred/python/bin/:$PATH
Recommended: Create a virtual environment
Due to the confusion mentioned in the previous section, it can be very helpful to create a virtual environment.
To learn more about virtual environments see my Virtual Environments Tutorial.
Here is a simple example of how to create a virtual environment.
# Create a virtual environment
cd $HOME
python3 -m venv myvenv
Then to activate the virtual environment, you could run the following command.
You can also add the line to your $HOME/.zshrc
file to load the virtual environment automatically on shell load.
# Activate virtual environment
source $HOME/myvenv/bin/activate
Conclusion
After reading this guide, you should have a good understanding of what methods are available to get Python 3 on your MacOS system as well as how to manage multiple versions.