Python 3 and Pip installation in macOS using Homebrew
I tried several approaches to installing Python 3 on Mac, and this is what worked as the best solution for these three goals:
- Latest Python 3 and Pip 3 available
- Both of them available in Bash under python and pip aliases replacing Python 2
- Aliases cannot break with updates
XCode
XCode is Apple?s development environment, and you need it?s command line tools. If you are not sure, don?t worry, the system is likely going to prompt you and allow you to install it without any hassle. If you want to be proactive, use your Apple ID to download it from https://developer.apple.com/download/more/. Search for command line and install the DMG package.
Homebrew
You will need to install Homebrew first to have it manage your Python. In your terminal run:
$ ruby -e “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)”
This may look like a scary command, but it?s safe, and it will guide you through the installation.
Python 3
Install Python 3 using Homebrew by running this command:
$ brew install python
This will make sure that your Homebrew is updated, python is the latest and so on. The installation itself can take a while and may require some simple troubleshooting that will be described below.
In theory, brew should have installed both Python 3 and Pip 3 and already put them under python and pip aliases. Verify that by these commands:
$ python -VPython 3.7.3$ pip -Vpip 19.1.1 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)
If that is what you see, then your work is done. Congratulations.
Troubleshooting
Homebrew Error
Your Homebrew threw this error while installing Python:
Error: An unexpected error occurred during the `brew link` stepThe formula built, but is not symlinked into /usr/localPermission denied @ dir_s_mkdir – /usr/local/FrameworksError: Permission denied @ dir_s_mkdir – /usr/local/Frameworks
Your computer is missing Frameworks directory. So let?s fix that:
$ sudo mkdir /usr/local/Frameworks$ sudo chown $USER /usr/local/Frameworks$ brew link python
Python & pip are still on version 2 or pip command is missing
If your python -V or pip -V returns version 2 or pip alias is missing altogether, we can fix that as well. You need to add correct aliases into your Bash ~/.bash_profile:
$ alias python=’python3’$ alias pip=’pip3’$ nano ~/.bash_profile
Into the file add the two alias lines from above. You need to both run them in the terminal and add them to the file to have the change available immediately and persistently.
Other issues
For your troubleshooting, one command that can help you is:
$ brew info python
That should provide you with some information about your Python installation by Homebrew.
Last words
If you find other issues and you figure out how to fix them, let me please know and I will update this guide.
In case you are looking for something cool to do with your new-found Python skills, try my other article How to build awesome Rasa chatbot for a web that requires minimal Python knowledge.