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


Have Questions? Post them here!
Advertisements
Try Out Code2care Dev Tools:

Advertisements

Advertisements
Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and software developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include learnings, tutorials, trouble-shooting steps, video tutorials, code snippets, how-to, blogs, articles, etc.

🎉 We are celebrating the 10th years of Code2care! Thank you for all your support!

We strongly support Gender Equality & Diversity.