Execute a Python script on macOS

There are five ways you can execute your Python script on macOS.

1. In IDLE

As you already know, you can execute your Python script in IDLE by pulling down
Run → Run Module

2. In the macOS Finder:
drag the script onto the Python Launcher.app

First find out where the executable file python3 was installed on your Mac. Launch
Applications → Utilities → Terminal.app
and say

which python3
/Library/Frameworks/Python.framework/Versions/3.6/bin/python3

exit

Now launch
Applications → Python 3.6 → Python Launcher.app
In the Preferences window, say
Settings for file type: Python Script
Interpreter: /Library/Frameworks/Python.framework/Versions/3.6/bin/python3
☑ Run in a terminal window
Then quit the Python Launcher.

You should now be able to launch your Python script in the macOS Finder by dragging the script onto the Python Launcher. A Terminal window will open, in which your Python program will run. The window will even display the exit status (zero) you passed to the sys.exit function.

3. In the macOS Finder:
double-click on the script’s icon

Select your Python script in the macOS finder, and type command-i for information about it. In the Info window, under
▼ Open with:
select Python Launcher 3.app (3.6.4).

You should now be able to launch your Python script by double-clicking on the script in the macOS Finder.

4. In the macOS Terminal:
feed the script to python3

Launch
Applications → Utilities → Terminal.app

If you are already in the directory that holds your Python script, you can simply give the name of the script to python3. (If you don’t know what directory that is, search for it with find and wait patiently.) If you are in a different directory, you can still run your script but you’ll have to tell python3 where your script is.

which python3
/Library/Frameworks/Python.framework/Versions/3.6/bin/python3

find / -type f -name inout.py 2> /dev/null
/Users/myname/python/inout.py

cd /Users/myname/python
pwd
/Users/myname/python

python3 inout.py
What is your first name?

python3 /Users/myname/python/inout.py
What is your first name?

5. In the macOS Terminal:
make the Python script itself executable

Edit the script (using IDLE or TextEdit.app) so that its first line is
#!/usr/bin/env python3
The first two characters on the first line must be #! And if you’re using a fancy editor, make sure you save the file as plain text, not rich text. It should look like this:

#!/usr/bin/env python3
"""
inout.py

Prompt the user for their age.
Then tell them how old they are in dog years.
"""

import sys

try:
    years = input("How old are you? ")
#etc.

Launch
Applications → Utilities → Terminal.app
and cd to the directory that holds your script. Use chmod to turn on the script’s r and x bits to make it readable and executable.

cd /Users/myname/python
pwd
/Users/myname/python

ls -l inout.py
-rw-r--r--@ 1 myname  mygroup  448 Jun  8 17:29 inout.py

chmod 755 inout.py
ls -l inout.py
-rwxr-xr-x@ 1 myname  mygroup  448 Jun  8 17:29 inout.py

./inout.py
How old are you?

The leading ./ tells the bash shell to execute the inout.py that’s in the user’s current directory.

Eliminate the ./

You can eliminate the need for the ./ by placing the inout.py in a directory that’s in your PATH. For example, I created a folder (directory) named /Users/myname/python and that’s where I put my Python programs. To see the directories that are already in your PATH,

echo $PATH
/Library/Frameworks/Python.framework/Versions/3.6/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin

echo $PATH | tr : '\n'
/Library/Frameworks/Python.framework/Versions/3.6/bin
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin
/opt/X11/bin

echo $PATH | tr : '\n' | cat -n
     1	/Library/Frameworks/Python.framework/Versions/3.6/bin
     2	/usr/local/bin
     3	/usr/bin
     4	/bin
     5	/usr/sbin
     6	/sbin
     7	/opt/X11/bin

When you installed Python 3 on your Mac, you automatically appended the following two statements to the .bash_profile file in your home directory. If you are confident in your ability to edit your .bash_profile without damaging it, you can edit this file with
Applications → TextEdit.app
When TextEdit shows you the names of the files in your home directory, press command-shift-> to see the files whose names start with a dot. Then double-click on .bash_profile. The last two statements in .bash_profile should be

# Setting PATH for Python 3.6
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.6/bin:${PATH}"
export PATH

Change them to

# Setting PATH for Python 3.6
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.6/bin:${HOME}/python:${PATH}"
export PATH

You should now be able to open a Terminal window and say

echo $PATH | tr : '\n' | cat -n
     1	/Library/Frameworks/Python.framework/Versions/3.6/bin
     2  /Users/myname/python
     3	/usr/local/bin
     4	/usr/bin
     5	/bin
     6	/usr/sbin
     7	/sbin
     8	/opt/X11/bin

which inout.py
/Users/myname/python/inout.py

inout.py
How old are you?