Making Python 3 Default in MacOS

Feb 15, 2022 - 5 Comments

Make Python 3 the default in macOS

Python users on the Mac are likely aware that Python is being deprecated from macOS 12.3 onward, and will no longer be preinstalled on the Mac. But Python remains an incredibly popular programming language, and if you rely on Python you’ll likely want to continue to have Python available in macOS. It’s easy to get Python 3 on the Mac, so let’s go a step further and show you how you can make Python 3 the new default Python version in macOS, whenever the python command is executed.

Installing Python 3 on the Mac

If you already installed Python 3 on the Mac using the official Python installer from here or Homebrew, you can skip this section.

If you haven’t installed Python 3 yet it’s easy to do so with a single homebrew command:

brew install python

This will install the latest Python 3 release available through HomeBrew. Again, you can also install it using the Python 3 installer, or even the MacAdmins Python release if you’d prefer.

How to Make Python 3 the Default in MacOS

We are assuming you’re using the default Zsh shell (or Oh My Zsh) and thus modifying .zshrc, but if you still use bash then you’d add the alias into .bashrc instead.

  1. From the Terminal, open zshrc in your text editor of choice, we’ll use nano for the sake of ease:
  2. nano ~/.zshrc

  3. Add the following alias at the bottom of the .zshrc file:
  4. alias python=/usr/local/bin/python3

  5. Hit Control-O and then Control-X to save the edit and then exit out of nano

Now you can confirm the alias worked by checking the python version:

$ python --version
Python 3.9.8

This should be the same response as if you type, since the python command has been aliased to python3:

python3 --version

Remember this is only an alias. If you continue to have the original Python 2.7.x release installed on the Mac, it remains accessible and usable by specifying the full path, like so:

/usr/bin/python

Keep in mind that in future versions of MacOS, Python 2.x will no longer be bundled.

We’re using aliases here in this example, but you can use a symbolic link linking /usr/bin/python3 to /usr/bin/python instead if you’d prefer to go that route.

What happened to Python in MacOS?

For those who aren’t aware, Apple has been warning for a while now that Python would be deprecated from future macOS versions, and that time has finally arrived with macOS Monterey 12.3 onward. This is found in the developer notes, listed under Deprecations:

Deprecations
Python 2.7 was removed from macOS in this update. Developers should use Python 3 or an alternative language instead. (39795874)

Thus if you are or were dependent on Python 2.7.x for any particular reason, you’ll either need to update your programs for Python 3 compatibility, maintain an older deprecated Python 2.x release (which is possible with Homebrew, etc), or rewrite everything and move on to another language entirely.

If you have any relevant thoughts, information, or experiences with Python in the latest macOS versions, share in the comments.

.

Related articles:

Posted by: Paul Horowitz in Command Line, Mac OS, Tips & Tricks

5 Comments

» Comments RSS Feed

  1. Glenn says:

    This technique only works when running python from the command line.

    If I have a program that begins with “#!/usr/bin/python” and I want it to run python3 it won’t work.

  2. Emilio says:

    Hey, You may want to share that you need to exit the terminal and restart it after changing the .zshrc in nano. Mine required a restart of the Terminal app to take affect :)

  3. CLI Nerd says:

    Easier way to add the alias to zshrc is to use echo and redirect:

    echo "alias python=/usr/local/bin/python3.7" >> ~/.zshrc

    No text editor needed :)

    And same for bashrc:

    echo "alias python=/usr/local/bin/python3.7" >> ~/.bashrc

  4. Keira says:

    There is a huge difference between “Python is being deprecated,” and “Python 2.7 was removed from macOS in this update. Developers should use Python 3 … instead.” I don’t understand why the story is written as if the former is true, when, in fact, it is not. It is the latter that is true.

    • Chaga says:

      Python is being removed from macOS, and won’t be preinstalled anymore, neither 2.7 or 3.9. That is a step beyond deprecated from macOS, but considering they won’t be including it any further… ehh deprecated sure that works.

      perl is supposedly getting the boot too FWIW.

      UNIX underpinnings, amirite? macOS becomes iOS with each update.

Leave a Reply

 

Shop on Amazon.com and help support OSXDaily!

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates

Tips & Tricks

News

iPhone / iPad

Mac

Troubleshooting

Shop on Amazon to help support this site