How to run Python file from Mac Terminal

In order to run a Python (.py extension) file from the Mac Terminal all you need to do is type python3 followed by the name of the Python file.

Example:
  1. Open Mac Terminal,
  2. Type,
    python3 <file-name>
    % python3 HelloWorld.py
    Hello World!
Run Python File using Mac Terminal

If you are using an older version of Python, then you may have to use python instead of python3

If Python is not installed on your Mac you will get an error,

% python3 HelloWorld.py 
zsh: command not found: python3

Comments & Discussion

Facing issues? Have questions? Post them here! We're happy to help!