Installing Python on macOS can be a bit of a journey, especially since the operating system comes pre-installed with an older version. Thankfully, Homebrew, a popular package manager for macOS, makes it straightforward to get the latest version of Python. Let’s walk through the steps together.
Why Upgrade Your Python?
Upgrading to a newer version of Python is essential for several reasons. Not only does it provide you with access to the latest features and security updates, but it also ensures compatibility with modern libraries and frameworks. In a world where technology evolves rapidly, staying current is not just a nice-to-have—it’s a necessity.
Getting Started with Homebrew
First, if you haven’t already, you’ll want to install Homebrew. It’s a simple and powerful way to manage software on your Mac. If Homebrew isn’t on your system yet, you can install it by running this command in your terminal:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Once Homebrew is ready, installing Python is a breeze. Just run:
brew install python
Accessing Your New Python Installation
After the installation, you’ll have both the older version of Python and the newer version side by side. To check which version you have, run:
python --version
You might see something like:
Python 2.7.16 # <- This is the old macOS version!
Now, let’s take a look at the version you just installed:
python3 --version
You should see:
Python 3.7.4 # <- This is the version we want
Using the Right Package Installer
When working with Python, you’ll also want to use the corresponding package installer. Instead of using pip, which is linked to the older version, you’ll want to use pip3:
pip --version
This command will likely show:
pip 19.1.1 from /usr/local/lib/python2.7/site-packages/pip (python 2.7)
Now, let’s check pip3:
pip3 --version
You should see:
pip 19.1.1 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)
Updating Your PATH for Convenience
To make your life easier, you may want to add Python 3.7 to your system’s PATH. This way, you can call it with just python instead of python3. Open your terminal’s configuration file, which could be either ~/.zshrc or ~/.bash_profile, and add the following line:
export PATH="/usr/local/opt/python/libexec/bin:$PATH"
If you’re coming from a bash environment, you may need to adjust your PATH accordingly to avoid conflicts.
Final Thoughts
After adding the necessary lines, simply restart your terminal or run source ~/.zshrc (or source ~/.bash_profile) to apply the changes. Now, when you type python, it should invoke the newer version by default.
In summary, upgrading Python on macOS through Homebrew is not just a technical necessity; it’s a step toward ensuring you have the right tools for modern development. Remember, clarity in your environment leads to clarity in your code. Happy coding!