Pro Terminal Commands: Run a Python Script on Mac


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 especially, which leaves a lot of folks looking for explanations on how to perform tasks that are apparently so simple, no tutorial thought to explain them. Run a Python script on macOS is often one of those things.

If you’re not familiar with the language, you can recognize scripts written in Python by their distinctive “.py” file extension. Python scripts can be executed in a couple different ways, depending on what tools you have at your disposal.

Run a Python Script With Terminal

This most universal method uses only built-in tools and utilities.

1. Take note of the path of the Python script in Finder.

If you’re not sure what the path is, you can find out easily. Right-click on the file while holding down the Option key and select “Copy … as Pathname” to copy the path to your clipboard.

1. Use the cd command to navigate to the folder containing the Python file. If you copied the pathname above, paste the pathname after the cd command. If you need a quick refresher on how cd works, check out our introduction to Terminal.

2. Type the following command to execute the script:

Of course, replace filename with the actual name of your script. In our example, we replaced it the filename with configure.py.

Run a Python Script with Python Launcher

By default, macOS comes with Python 2.7.3 installed. That’s not the newest version of Python, however. If you’ve installed Python 3, you’ll have access to a few more software tools. These include the Python Launcher, a GUI program that runs Python scripts. If it is installed on your Mac, you can run Python scripts from Finder.

1. Locate the Python script file in Finder.

2. Right-click the file and select “Open With -> Python Launcher” from the context menu.

Alternatively, Python scripts can be dragged onto the Python Launcher icon. This works with the Python Launcher icon in both the Dock and in the Applications folder.

Configuring the Python Launcher

You can also adjust the Python Launcher with a few additional options. With these options, you can select your version of Python, enable debugging tools, and adjust how the script runs. These options will be mostly of interest to those with some knowledge of the Python programming language.

The Python Launcher 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 menubar.

The preferences window can also be invoked before running a script. Drag the script on the Python Launcher icon while holding the option key, and the preferences menu will spawn before the script is run, allowing you to adjust settings that will only apply to this run of the script. Typically, changes to the Python Launcher preferences are global, affecting all future scripts.

Run a Python Script with IDLE

Python files can also be run within the Python IDE, IDLE. For Python programmers working within the integrated development environment, this makes sense. But if you’re just looking to quickly launch a script, it’s like lighting a cigar with a cruise missile. Impressive, perhaps, but not what you might call “necessary.”

Getting Started with Terminal: An Introduction

Create a RAID Array on macOS Sierra

Installing and Using Python 3 On Your Mac


Alexander Fox

0 Comments

Your email address will not be published.