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.
Confirm your Python version
Although Python 2 is installed by default on Apple computers, Python 3 is not. You can confirm this by typing in Terminal
python --version and hitting Enter:
$ python --version Python 2.7.13
To check if Python 3 is already installed try running the command
python3 --version. Most likely you’ll see an error message, but it’s worth checking. Even if you have a version of Python 3, we want to be on the most recent release, which is 3.6.3 at this point in 2017.
Install Xcode and Homebrew
We will use the package manager Homebrew to install Python 3. Homebrew depends on Apple’s Xcode package, so run the following command to install it:
$ xcode-select --install
Click through all the confirmation commands (Xcode is a large program so this might take a while to install depending on your internet connection).
Next, install Homebrew:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Note: You can also find this command on the homepage of the Homebrew website. It’s easiest to copy and paste since it’s a long command.
To confirm Homebrew installed correctly, run this command:
$ brew doctor Your system is ready to brew.
Install Python 3
To install the latest version of Python, run the following command:
$ brew install python3
Now let’s confirm which version was installed:
$ python3 --version Python 3.6.3
To open a Python 3 shell from the command line type python3:
$ python3 Python 3.6.3 (default, Jul 17 2017, 16:49:06) Python 3.6.3 (default, Oct 4 2017, 06:09:15) [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.37)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>>
When you want to exit, type
exit() and then Return or
Ctrl-D (the Control and D key at the same time).
Note you can still run Python shells with Python 2 by simply typing
$ python Python 2.7.13 (default, Dec 18 2016, 07:03:39) [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin Type "help", "copyright", "credits" or "license" for more information.
Bonus: Virtual Environments
You should always use a virtual environment for your Python projects. A virtual environment is a way to create an isolated space so you can, for example, run Python 2.7 for one project and Python 3.6 for another on the same computer. We can use the built-in venv module for this.
It’s a best practice to keep all your virtualenvs in one place, for example
.virtualenvs/ in your home directory. Let’s create that directory:
$ mkdir ~/.virtualenvs
Now create a new virtual environment called
myvenv by running:
$ python3 -m venv ~/.virtualenvs/myvenv
Because we used
python3 here our virtual environment knows that when we type
python for a command we mean Python 3 not Python 2. In order to activate this virtual environment, so we can use it, we must also run the following command:
$ source ~/.virtualenvs/myvenv/bin/activate (myvenv) $
Note that while the environment is active, you will see its name in parentheses. Software packages we install now will only be available within this virtual environment. You can use the command
pip freeze to see all installed software within a virtual environment.
To stop using a virtual environment, either close the Terminal window or enter
(myvenv) $ deactivate $