Python is a language that is very close to the Mac. It rejects many syntax practices of traditional programming languages in favor of human-readable code, tab-based hierarchy, and abstract memory management. It’s a great first programming language, although there are some tasks that aren’t covered in many tutorials, like running a Python script in macOS.
In this article, we’ll show you two different ways to run a Python script on MacOS, even though the first is rarer than the second.
For those who don’t know, Python scripts use the .py file extension, regardless of the language version you’re using.
By default, macOS ships with Python 2.7.3. However, we do not recommend using this version because it is now obsolete.
A common way to install Python 3 is to use a separate installer. This gives you access to tools such as Python Launcher – a graphical user interface (GUI) program for running Python scripts from the Finder.
To do this, locate the Python script file in the Finder, right-click, and search for the path name using “Get Data.”
Then right-click on the file and select “Open -> Python Launcher” from the shortcut menu.
Python scripts can also be dragged to the Python Launcher icon in either the Dock or Applications folder.
Configuring the Python Launcher
If you want to use Python Launcher, you can customize it with a few advanced features. For example, you can select a version of Python, enable debugging tools, and adjust script behavior.
The Python Launcher Settings window automatically opens in the background while Python Launcher is running. You can also open it manually by selecting “Python Launcher -> Preferences” from the Python Launcher menu bar.
You can even launch the Settings window before running the script. To do this, drag the script to the Python Launcher icon while holding Option and the Settings menu appears before running the script.
This allows you to adjust settings that apply only to this script execution. It is almost like a temporary runtime installation, although changes to Python bootloader settings under normal circumstances are common, affecting all future scripts.
Using the terminal
Most users run scripts from a terminal, which is a good way to run a Python script on MacOS.
First, you need to find the path to the Python script in the Finder. If you’re not sure, you can Option + Right-click the file and select “Copy … with path name” to copy the path to the clipboard.
You can also drag a file to the terminal window to see its absolute path.
The terminal-centric method uses
cd to move to the folder that contains the Python file. Note that you can also paste the copied path name here.
Once you are in the right folder, use
python3 execute the script.
here represents the name of your script. It’s a simple approach you’ll learn if you use Python regularly.
Using Python files on a Mac is easy, especially considering some of the built-in features. As such, there are two ways to run a Python script in MacOS: during Python Launcher and the more general Terminal run. Using a terminal is the recommended way because you often use Homebrew to install the latest version of Python 3 (instead of using the built-in and outdated Python 2).
If you’re using a Windows machine, Python 3 is also easy to install and use. Do you have a method for running a Python script in macOS that we haven’t covered? Tell us in the comments section below!