Starting the switch to python 3

With Python (and other scripting languages) being dropped from deprecated in macOS Catalina*, and Python 2.7 maintenance ending in 2020, it’s time to start updating any Python 2 scripts so they are compatible with Python 3.

This might seem like a big task, and it could very well be for your code-base, however there are a few things you can do to start the process.

If you don’t already have Python 3 installed on your Mac, you’ll need to get it installed.

You can install it using homebrew, or you can install it from the Python Releases for Mac OS X page. It’s also provided by Apple, but currently you need an Apple Developer account to access those command line tool betas.

There’s a great guide on what to do and what not to do when installing Python 3 on macOS here.

No matter which way you get it installed, your Python 3 interpreter will be in a new location, and you’ll definitely want to update pip3 for package management.
You can also expect a lot, if not all of the “standard” packages that Apple included with their Python 2.7 framework to not be installed, so you’ll need to make sure you’ve installed those as well. This is a great opportunity to start using virtual environments.

For the process of migrating your code, there’s a very handy quick start guide, as well as a lot of in depth coverage about. There is a process to automatically update Python 2 code to Python 3 compatible code, which is covered here.

Test all the changes made, particularly those made by futurize, to ensure that the scripts function as expected and that no “undocumented features” are present.

A few things that are important:

  • A homebrew installation of Python 3 might install it to different locations as compared to installing Python 3 from the official packages.
  • Python 3 won’t necessarily end up in your path, so you’ll either need to add it to your path, or set up some aliases to relevant binaries.
  • Using #!/usr/bin/env python as the shebang in your script doesn’t automatically mean a script will be executed with the Python 3 interpreter.
  • Python 3 installed by the official Python installer is not code-signed or notarised.

*Correction (2019-06-29): Python 2 is deprecated and will be removed in a future release of macOS. That could be a future dot release of macOS Catalina, or even later.