Install Python 3 on Mac OS X
This short tutorial will show you how to properly install Python 3 on a Mac OS X computer. There are multiple ways to install Python 3, including a download from the official Python site, however I strongly recommend instead using a package manager like Homebrew to manage all your dependencies going forward. It will make your life a lot simpler.
The first step is to install Xcode, which is pretty much mandatory for doing development work on an Apple computer. You can install Xcode here.
As part of the install process Xcode will ask you to login with your Apple ID and agree to the Xcode release agreement.
Make sure to also install the Command Line Tools, which for some reason Xcode does not install by default. Open your Terminal application (Finder -> Applications -> Utilities -> Terminal, type the following and hit Return:
This will trigger a pop-up window asking you to install the Command Line Tools. Do so.
Homebrew is a popular package manager that helps install and update software correctly. If you downloaded Python 3 directly from Python.org there’s no guarantee it won’t conflict in some way with other software on your machine. Furthermore, you have to manually update it every time there’s a new release. With Homebrew, you can use one command to check for updates for all your managed software.
To install Homebrew copy and paste the following into Terminal and hit Return:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Now we need to update our $PATH environment variable so our computer knows where to find software we’ve downloaded via Homebrew. For example, let’s say I download two different versions of Python and store the code in different folders. When I type “python” on the command line, how does the computer know which version of Python I want to use? The $PATH environment variable is where we can explicitly tell the computer where to look.
Type the following into your Terminal and hit Enter:
We’re using nano, which is a built-in text editor, to open the file
.bash_profile. Nano is a text-only editor so your mouse will not work here. Use the up/down and left/right arrows on your keyboard instead to navigate. At the top of the file create a new line and type the following:
We’re telling the computer to look in the directory /usr/local/bin/ for software packages. This is where Homebrew installs things.
Now hold down Control and the “X” key to Exit and say Yes to saving the file.
For the changes to go into effect we need to close Terminal completely (Command + Q) and then open it again (Finder -> Applications -> Utilities -> Terminal).
In our new Terminal window type the following and Return to confirm our changes:
You should see
/usr/local/bin: at the beginning of a long string of paths. Don’t worry about the rest of them.
Every Apple computer, by default, comes installed with a version of Python 2. To confirm this, type the following:
But we want to use Python 3 and in fact we can use both Python 2 and Python 3 on our machine. To install Python 3 using Homebrew, type the following:
brew install python3
To confirm Python3 is installed, type:
If you want to execute a command using Python 2 you just type “python” and for Python 3 type “python3.”