This post is about how to install Python on macOS. The intended audience is a developer, either beginner or advance, basically someone used to type commands in the terminal and some understanding of a UNIX shell.
This is my personal configuration. There are many ways to accomplish this, and there isn’t a definitive way to be the right one. Basically macOS comes with Python 2.7 installed. This version is deprecated and there’s no more support for it. If you are new to Python, just focus on Python 3. There’s still a lot of code in 2.7 but if you are new to the language, there are very little chances you will end up working with old code, mostly for maintenance or to upgrade it to version 3, which is something that would require a more experienced developer, so for now and most probably in the future to come as a Python developer, just focus on getting Python 3 under control.
- Level: Beginner
- Platform: macOS 10.15
- Intended audience: Developer
We don’t want to mess up with the default configuration of macOS, also we don’t want that our changes might be overwritten by a future update. macOS doesn’t really have a package manager, so you need a third party one. I personally use homebrew. There are others, like MacPorts, which I used to be an user before, but now I personally prefer homebrew. I find it cleaner, faster and more updated than MacPorts, but things maybe changed since last time. So if you don’t have a package manager yet, go ahead and install homebrew by opening a terminal and copy the following command:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
It will ask for your password and just follow the instructions on the terminal. If you don’t have Xcode installed, it will download and install the Xcode command Line Tools. It might take a while. If it fails installing them, you can always install them manually. For that you need an Apple developer account. Just to create the account is free. Most certainly you already have an Apple ID, you can use that one. Go here and search for the Command Line Tools. Don’t get the beta one of course!
If all went well, you should have homebrew installed. You can directly install Python 3 using homebrew, but we can do better. Let’s install pyenv. It’s a Python version management tool. As you get deeper learning Python, you will deal with different versions and try new things, so better to have a tool to make this task easier and avoid messing around with your system configuration. To install it, just type:
brew install pyenv
Let’s install the latest version of Python at the moment 3.9 and also 3.7 so you see how to use pyenv
pyenv install 3.9.0 pyenv install 3.7.7
If you want to see all the available versions to install, type:
pyenv install --list
After installing 3.9.0 and 3.7.7 confirm they are available with:
zuco@devmini ~ % pyenv versions * system (set by /Users/zuco/.pyenv/version) 3.7.7 3.9.0 zuco@devmini ~ % python --version Python 2.7.16 zuco@devmini ~ %
As you can see in the output, pyenv versions listed the versions but doesn’t have one selected as global yet, you can see that the * points to the system one, which is the default with macOS. In fact after typing
python --version, we are still getting 2.7.16
In order to set our global python version, we need to type:
pyenv global 3.9.0
zuco@devmini ~ % pyenv versions system 3.7.7 * 3.9.0 (set by /Users/zuco/.pyenv/version) zuco@devmini ~ % python --version Python 2.7.16
But we are still getting 2.7.16 as our Python version. Even if pyenv is pointing to 3.9.0, we didn’t configure our shell’s path to use pyenv. You can add the following code in your .zshrc or .bash_profile:
if command -v pyenv 1>/dev/null 2>&1; then eval "$(pyenv init -)" fi
Or type the following command. Change .zshrc by .bash_profile if you are using bash:
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.zshrc
To load the configuration, use source, use.bash_profile if you are using bash:
zuco@devmini ~ % python --version Python 2.7.16 zuco@devmini ~ % source .zshrc zuco@devmini ~ % python --version Python 3.9.0 zuco@devmini ~ %
As you can see in the output, before loading the environment, our default Python version was still 2.7.16, after reload our shell configuration, the default Python version is 3.9.0
From now on, if we want to use a different version of Python we just execute
pyenv global <version> and every time we type
python, it will point to the right one