Using Applescript together with Python in macOS 12.3 (and higher)

Apple has removed Python 2.7 from macOS in version 12.3 (macOS Monterey 12.3 Release Notes)

If you are using Applescript together with Python, i.e., calling Python commands or a Python script from within an Applescript, e.g.,

set theShellScript to "/usr/bin/python <your python command>"
set TheResult to do shell script theShellScript

you need to

  1. reinstall Python (python.org, anaconda.com)
  2. get the path of your new Python installation: open a terminal and call which python
  3. and use the shown path instead of “/usr/bin/python”

E.g., if you install Python via Anaconda, the new Python path could look like /Users/<YouUserName>/opt/anaconda3/bin/python.

4 Likes

Thanks for the heads-up on this. :slight_smile:

1 Like

People should keep in perspective that Python 2.7 was “sun-setted” by Python Foundation to be effective 1 Jan 2020 (more than 2 years ago). See Sunsetting Python 2 | Python.org where they explain it. The intention of this deprecation was announced many years before, and officially the date for “last version” set in 2019. As they say, time for Python people to move on.

The above is a great explanation of how to reinstall Python 2.x in an outstanding virtual environment, Anaconda, which becomes independent of the macOS operating system offering of Python 3.x. If you must keep using Python 2.x, this a good way to do it. I understand how avoiding change is preferred sometimes. There are other virtual environment applications out there for you to discover.

It’s also a good (and highly recommended) that you install and move to your own Python 3.x environment on your Mac. Using Anaconda for this by setting up another virtual environment to do so is a good idea. You have, thus, both Python 2.x an 3.x available to you without interfering with the version Apple provides.

In my view, and the view of many others, that you should keep you Python environments seperate from the Apple version so that as you add libraries to Python (truly there are so many valuable and useful ones) you add them to your version in the virtual environment (Anaconda or other). Apple provides Python for their own uses in macOS as a number of their services depends on it. It is risky to mess with the system-provided Python.

Meantime, best to migrate your stuff off Python 2.x to 3.x and beyond. Python provides some good tools to make that simple if your systems are complex.

I’ve used Anaconda for many years, and Python for many years before that for small/simple and big/complex solutions. Highly recommended.

1 Like

Please please update your code to Python 3.
Your code should not be using Python 2.x anyway, as suggested for several years now by the Python team.

2 Likes

Python 3 isn’t actually installed by default on a fresh installation of Monterey. Trying to run Python for the first time actually runs a script to install Python 3.

1 Like