How to Run a Python Script on Mac

Python is a highly Mac-like programming language. Human-readable code, tab-based structure, and abstracted memory management replace many of the grammar norms of traditional programming languages. It’s an excellent introductory programming language, albeit some activities aren’t addressed in many courses, such as to run a Python script on Mac.

Run a Python Script on Mac

We’ll show you two ways to launch a Python script on macOS in this post, but the first is less popular than the second.

Read Also: How to Run Shell Script on Mac

Python Launcher

Python scripts, regardless of the version of the language you’re using, have file extension.

Python 2.7.3 is installed by default on macOS. However, because this version is now deprecated, we do not advocate utilizing it.

The dedicated installer is a common way to install Python 3. This allows you to use tools like the Python Launcher, which is a graphical user interface (GUI) program that lets you run Python programs from the Finder.

To get the pathname, locate the Python script file in the Finder, right-click it, and select “Get Info.”

Get Info menu

Then, from the context menu, right-click the file and select “Open With -> Python Launcher.”

Python programs can also be dragged and dropped onto the Python Launcher icon in the Dock or the Applications folder.

Configuring the Python Launcher

If you enjoy how the Python Launcher works, you can tweak it with a few more choices. You can, for example, choose your Python version, enable debugging tools, and adjust how the script runs.

Preferences menu to run Python script on Mac

When you run Python Launcher, the Preferences box appears in the background. You can also open it manually by going to the Python Launcher menu bar and selecting “Python Launcher -> Preferences.”

Even before running a script, you can open the Preferences box. To do so, hold down the Option key when dragging the script to the Python Launcher icon, and the Preferences’ menu will appear before the script runs.

This will allow you to change options that will only apply to this script run. It’s almost like a temporary run setting, however, under typical conditions, changes to the Python Launcher Preferences are global, impacting all future scripts.

Using the Terminal

The Terminal, which is a fantastic way to run a Python script on macOS, is where most users will run scripts.

To begin, open the Finder and look for the Python script’s path. If you’re not sure, you can copy the path to your clipboard by pressing Option + Right-Clicking on the file, then selecting “Copy… as Pathname.”

Copy as Pathname option to run a Python script on Mac

To see a file’s absolute path, drag it onto the Terminal window.

Dragging a file onto the Terminal window

The cd command is used in the Terminal-centric method to navigate to the Python file’s folder. It’s worth noting that you can also paste your copied path name here.

Navigating to a directory within the Terminal

Once you’ve found the correct folder, run the script with python3 <file name>

Running a Python script on Mac from the terminal

Of course, this is where your script’s name goes. It’s a simple strategy that you’ll get used to if you use Python on a daily basis.

Conclusion: Run Python Script on Mac

On the Mac, working with Python files is a breeze, especially with some of the built-in features. As a result, there are two ways to run a Python script on macOS: using the Python Launcher app or using the Terminal. Because you’ll frequently use Homebrew to install the latest Python 3 version, using the Terminal is the preferable method (rather than use the built-in and obsolete Python 2).

Python 3 is also simple to install and use on a Windows system. Do you know of another way to launch a Python script on macOS that we haven’t covered? Please share your thoughts in the comments box below!

Is this information helpful?

Leave a Reply