How to 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. Running 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.

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

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

run-python-script-macos-finder-python-script-location

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.

run-python-script-macos-finder-copy-path-name

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.

run-python-script-macos-terminal-cd-to-folder

2. Type the following command to execute the script:

run-python-script-macos-terminal-run-python-script

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

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.

run-python-script-macos-finder-python-script-location

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

run-python-script-macos-python-launcher-open-with-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

run-python-script-macos-python-launcher-preferences

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.

Python files can also be run within the Python IDE, IDLE. For Python programmers working within the IDE, 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.”

Leave a Reply

Yeah! You've decided to leave a comment. That's fantastic! Check out our comment policy here. Let's have a personal and meaningful conversation.