How to install Python 3 on your Mac in 5 easy steps!


When I recently switched over from my Windows PC to an M1 MacBook Air, I realized I also needed to transfer the Python scripts I wrote for discovering and reporting news at The 8-Bit.

I thought it would be an easy task as I’d simply install Python and then change file paths within my code to match macOS’ filesystem. What I took for granted is the possibility that it would be a nightmare to install and set up Python on my Mac.

The problems I faced…

Macs come with multiple versions of Python pre-installed. That’s essential for utmost compatibility. But it isn’t so much when you’re planning on working with Python for your projects.

Modern applications and libraries require the latest version of Python, which, at the time of this publication, is 3.9.4. While you can also work with recent versions like Python 3.8, the latest version is recommended for utmost compatibility.

The Python version that comes pre-installed in macOS is 2.7. There are older versions as well, but 2.7 is the latest pre-installed version. The problem is it’s deprecated and is not recommended for use in new projects.

Naturally, a clear course of action would be to delete the old version of Python and install the latest version from Python’s official website. But that might open up a whole new can of worms as some functions within macOS might be dependent on these old versions of Python.

So, knowing that, I went along to download the newest version of Python—3.9.4—and installed it directly by following the instructions in the executable file. Once I did that, I downloaded Microsoft’s Visual Studio Code editor and set it up to work with Python. Just to test it out, I wrote a simple “Hello, World” Python application and tried to save it.

That’s when VS Code pushed me to install Pylint, which is a source code, bug, and code quality checker for Python. I tried installing it from within VS Code itself, and it installed properly.

Fine.

Now, I needed to install the BeautifulSoup library for one of my web scraping projects. Prior to that, it was imperative to install pip. So, I opened the good ol’ Terminal and typed this:

python3 get-pip.py

Thinking I was set, I now tried installing BeautifulSoup using pip. Here’s the command I used:

pip install beautifulsoup4

It installed correctly.

Or so I thought.

When I tried importing BeautifulSoup in my code, I got hit with this error:

ImportError: No module named 'bs4'

That’s weird, you’d think.

Apparently, the new version of Python that I installed from the official website also somehow installed an older version—3.8—and the libraries I downloaded were installed inside that. inside that. However, even though I copied them inside the Python 3.9 folder within “/System/Library/Frameworks/Python,” when I typed in “python3 filename.py” in the Terminal, I was hit with the same error.

I am yet unsure as to what was causing the issue, but this is what I think happened. While Python 3.9 was the main Python installation on my Mac, when I called in the “python3 -version” command within the Terminal, it would return Python 3.8 as the version that’s installed.

I tried deleting the Python 3.8 installation folder as well, but unfortunately, it kept prevailing.

Defeated, I turned to the internet for help where I learnt of something called as pyenv.

What is pyenv?

It’s a virtual environment for Python. You can host multiple instances of Python and easily switch between them using pyenv. The best part, however, is that it’s set up in a way that individual Python installations are completely isolated from each other such that one version cannot interfere with another.

The added benefit is you can also test out your code on multiple versions of Python simultaneously. Pyenv is nothing short of a game-changer when it comes to Python. And I would absolutely recommend you use Python for development purposes.

Here’s some of the things that pyenv can do, according to its Github page:

  • Let you change the global Python version on a per-user basis.
  • Provide support for per-project Python versions.
  • Allow you to override the Python version with an environment variable.
  • Search commands from multiple versions of Python at a time. This may be helpful to test across Python versions with tox.

That said, it makes sense to install and use Python using pyenv. In this article, I’ll show you how to install and set up pyenv, and then install and set up Python within pyenv.

How to install Python using pyenv on Mac

In order to install pyenv on Mac, it’s best to use Homebrew, which is a popular Mac package manager that makes installing developer packages easy. It was also recently updated to natively support M1 Macs. Unfortunately, Homebrew does not come pre-installed on macOS. In order to install it, you need to use the Terminal. So, if you aren’t familiar with using a Terminal, follow the steps mentioned below as is.

Step 1: Installing Homebrew package manager

  1. Head over to Homebrew’s official website: brew.sh
  2. The website offers a handy command upfront that you can simply copy and paste into the Terminal. You can either copy that command manually or simply press the copy button beside the command as shown in the image below:
  1. Next, you need to open the Terminal. You can do that by either heading over to Launchpad, using Spotlight to search for it or going to the Applications folder in Finder.
  2. Once you’ve opened the Terminal, paste the Homebrew command that you copied earlier and press Enter.
  3. The Terminal will as for your system password. You won’t be able to see the password as you enter it, so make sure you enter it carefully. Once you’ve entered the password, press Enter once again.
  1. When you hit Enter after typing in your password, Terminal will ask you whether you want to proceed to install the dependencies required for Homebrew to run properly, hit Enter once again.
  2. Now that the Homebrew installation has officially kicked off, Terminal should ask you to enter your administrator password one last time.
  3. Once you’ve entered the password, the Terminal will install Homebrew on your Mac. This process might take some time depending on your Mac as well as your internet connection.

Step 2: Installing pyenv

  1. Head over to the Terminal once again. There, type in the command mentioned below and hit Enter:
brew install pyenv
  1. This will install pyenv to your Mac automatically.

Step 3: Setting up pyenv

Now that you’ve installed pyenv successfully, we need to set it up so that it is the default option when we open the Terminal the next time for using Python.

  1. In the Terminal, type the following command:
pyenv init
  1. Pyenv will ask you to append the code it provides to the file “~/.zshrc” which you do right within the Terminal. Copy the code as shown in the image below. The file in question might be different based on your macOS version, particularly if you’re running anything older than macOS Catalina. Regardless, pyenv will display that filename in the Terminal instead, which is what you should use.

You can also copy the code from here instead. You will need to paste this later:

eval "$(pyenv init -)"
  1. Now, type in the following command or copy it based on your convenience. Paste it in the Terminal, and hit Enter.
nano ~/.zshrc
  1. This will open up the nano text editor which is entirely based within the Terminal. Paste in the code you copied before into the blank space. Then, press CTRL + X to save the changes you made to the text file. Then, when the editor asks if you want to save the changes, hit “y” and finally, hit Enter.
Editing the ~/.zshrc file in Nano text editor within Terminal.
  1. Pyenv should be successfully set up on your Mac now.

Step 4: Installing Python using pyenv

  1. Head over to the Terminal and copy the following code there, then press Enter:
pyenv install 3.9.4

Note: In the above code, “3.9.4” denotes the version number for Python. It is recommended to install the latest version of Python. At the time of publication, the latest version of Python was 3.9.4. Make sure you head over to Python’s official website and note the latest version that’s available for you.

  1. Pyenv will install Python automatically based on the version number you entered. Once that’s done, we need to configure Python to use this version by default whenever we use the Terminal.

Step 5: Configuring Python to set it as a default version the next time you use the Terminal

  1. Copy and paste the following command into Terminal and hit Enter.
echo 3.9.4 > ~/.pyenv/version

Note: Replace “3.9.4” with the version of Python you installed earlier.

That’s it! You’ve successfully installed Python on your Mac.

Final thoughts

MacBooks are said to be one of the best machines for programmers, yet as it goes with machines, it’s normal to face hiccups now and then. If only I’d known about the pyenv method of installing Python, it would’ve saved me a ton of time that I otherwise spent researching how to do it.


Note: This story contains affiliate links that may earn The 8-Bit commissions on successful purchases to help keep the site running.