Python is a very Mac-style language. It ditches many of the syntax conventions of traditional programming languages in favor of human-readable code, tab-based hierarchy, and abstracted memory management. It’s a great first programming language, although there are some tasks not covered in many tutorials – for example, running a Python script on macOS.
In this post, we show you two different ways to run a Python script on macOS, though the first is less common than the second.
For the unaware, Python scripts use the .py file extension regardless of the version of the language you use.
By default, macOS comes with Python 2.7.3 installed. We don’t recommend using this version, though, as it’s now deprecated.
A common way to install Python 3 is through the dedicated installer. This gives you access to tools such as the Python Launcher – a Graphical User Interface (GUI) program to run Python scripts from the Finder.
To do this, locate the Python script file in the Finder, right-click, and use “Get Info” to find the path name.
Next, right-click the file and select “Open With -> Python Launcher” from the context menu.
Python scripts can also be dragged onto the Python Launcher icon, whether it’s in the Dock or the Applications folder.
Configuring the Python Launcher
If you like using the Python Launcher, you can customize it with a few additional options. For example, you can select your version of Python, enable debugging tools, and adjust how the script runs.
The Python Launcher’s Preferences window opens in the background automatically when Python Launcher is executed. You can also open it manually by selecting “Python Launcher -> Preferences” from the Python Launcher menu bar.
You can even invoke the Preferences window before running a script. To do this, drag the script to the Python Launcher icon while holding the Option key, and the Preferences menu will display before the script is run.
This will let you adjust settings that will only apply to this run of the script. It’s almost like a temporary run configuration, though changes to the Python Launcher Preferences under normal circumstances are global, affecting all future scripts.
Using the Terminal
Most users will run scripts from the Terminal, which is a stellar way to run a Python script on macOS.
First, you’ll want to find the path of the Python script in the Finder. If you’re not sure, you can Option + Right-Click on the file, then select “Copy … as Pathname” to copy the path to your clipboard.
You can also drag a file onto the Terminal window to see its absolute path.
The Terminal-centric way uses the
cd command to navigate to the folder containing the Python file. Note that you can paste your copied path name in here too.
Once you’re at the right folder, use
python3 <file name> to execute the script.
<file name> here represents the name of your script. It’s a straightforward approach and one that you’ll become familiar with if you’re using Python on a regular basis.
Working with Python files on the Mac is a breeze, especially given some of the built-in functionality. As such, there are two ways to run a Python script on macOS: through the Python Launcher app and the more common Terminal execution. Using the Terminal is the preferred way, as you’ll often use Homebrew to install the latest Python 3 version (rather than use the built-in and obsolete Python 2).
If you’re on a Windows machine, Python 3 is also a snap to install and use. Do you have a method for running a Python script on macOS we haven’t covered? Let us know in the comments section below!
Our latest tutorials delivered straight to your inbox