macOS – How To Run Python Script On The Terminal?

MacOS How To Run Python Script On The Terminal?

Python is a high-level, interpreter-based language. Python has 100s of vast libraries that provide functionalities like no other language. These Python libraries focus on different fields like data analysis, machine learning, web development, computer vision, and a lot more. All these libraries are incredibly lucid and have quite a short syntax. The applications of these libraries are phenomenal and allow you to install and import them into your modules with a single line of code. All these properties of Python make Python a perfect choice for a lot of projects.

What’s a Python script?

A Python script is a Pythonic text/code, i.e., a text, when read by the Python interpreter, has meaning to it. A Python script can be written in a file with the .py extension or can be executed directly in the terminal. A Python script can be a single line of Python code, or it can be a complex 100 lines of code with functions depending on each other. A Python script can be used to do a lot of things like automation of tasks, web development, data analysis, machine learning, solving differential equations, and many more.

Related: How to Build a Single Python File From Multiple Scripts?

What is a Terminal?

A terminal is a MacOS command line interface (CLI) application that can be used to execute OS-level operations and run system commands. The terminal can be used to run code too. A language like Python or JavaScript’s node.js allows you to write and run code directly from the terminal. So the terminal can also be used to run Python code. We can use the Python command the run a Python file or directly write Python script and receive output in the terminal.

Python shell command

The Python command can be used to run Python files or even write Python scripts in the terminal. Let’s check out how we can do it.

Executing a Python file

To execute a Python file in the terminal, we will first have to create a Python file. To create a Python file, just save a text file with the .py extension instead of the .txt extension.

Python File
Python File

Once you’ve created a Python file, go to the terminal and run the following command.

python <filename>.py
Running A Python File
Running A Python File

That was it. You just created and executed a Python file.

Now sometimes, you may not want to create an entire file but just want to run some piece of Python code that you won’t need later. To do so, you can write the code directly in the terminal. Python allows you to also write and execute code directly from the terminal itself. Let’s see how we can do it.

How to run Python script directly from the terminal?

To do so, you would need to open the terminal and the single-word "python" command in it.

Python In Terminal
Python In Terminal

When you do so, you can see that the version of Python in your system is shown. After that, instead of the new command line that you usually see in shell once a command is executed didn’t show up. Instead, a different entry point is shown. You can write Python code here. After writing the code once you press enter, the code will be executed directly. If you want to exit from the Python interface and get back to the new command line, you can use the exit() function.

Python Script From Terminal
Python Script From Terminal

How to find the path of the Python script file in the Finder and copy it to the clipboard?

To run a Python file using the Python command, you’ll need the exact location of the file in your system. To get the file path of a Python file, we will have to follow the following steps

  • Find the file whose path you want in Finder.
  • Two finger tap on it to open the context menu.
  • Now you’ll see the copy option in it. You have to press and hold the option key now. Doing so will change the copy option to copy file path option.
  • Now you can click on it to copy the file path to your clipboard.
Copy Option In Mac
Copy file path Option On Mac

How to make a Python script executable

If the Python file that you’re trying to run is not executable by default, you’ll have to make it executable using the shebang line. It’s a comment line that specifies the interpreter. Using the shebang line, you can execute a Python script directly without specifying the interpreter.

The shebang line

The shebang line can be written at the start of a Python file to make it executable. The shebang line looks like this:

#!/usr/bin/env python

Now you can change its permission to executable mode using the following chmod command.

chmod +x <filename>.py

After this, your file is turned into executable mode. Now you can run the file without the Python command in your terminal. You can use the ./ command to run the file now.


Python launcher app

The Python launcher app is an app that gets downloaded when you install Python on your Mac. It can be used to manipulate how the Python files are supposed to run. You can change the command to run Python files, handle the shebang line, enable debugging tools, etc.

Python Luncher App
Python Launcher App

Applications of Python script

Python is a versatile language. Python has a lot of applications and uses. Python doesn’t limits to 1 or 2 fields. Python script can be used to do simple things like printing hello world, building small desktop or mobile applications like calculators or making big projects like full stack web applications, making machine learning models, performing data analysis, web scraping, solving complex differential equations, and a lot more.

You can use web frameworks like Django and Flask to build web applications, Tkinter to build desktop applications, kivy for mobile applications, sklearn for machine learning, numpy, pandas, and matplotlib for data science, and sciPy for performing complex algebraic calculations.


MacOS is a powerful OS but quite hard to get a grip on. It was a lot of simple functionalities. At the start, it might get hard to understand it completely, but once you start using it, slowly, you begin to understand everything. Learning more and more will give you greater knowledge and help you do tasks with great efficiency.


Official Python Documentation.

Stack Overflow thread for the same question.