How to Run a Python Script via a File or the Shell

One of the most important skills you need to build as a Python developer is to be able to run Python scripts and code. This is going to be the only way for you to know if your code works as you planned. It’s even the only way of knowing if your code works at all!

This step-by-step tutorial will guide you through a series of ways to run Python scripts, depending on your environment, platform, needs, and skills as a programmer.

Generally programmers write stand alone scripts, that are independent to live environments. Then they save it with a “.py” extension, which indicates to the operating system and programmer that the file is actually a Python program. After the interpreter is invoked, it reads and interprets the file. The way Python scripts are run on Windows versus Unix based operating systems is very different. We’ll show you the difference, and how to run a Python script on Windows and Unix platforms.

Run the Python command-line interpreter, under your OS of choice,

Run any plain text editor

Run a Python script under Windows with the Command Prompt
Windows users must pass the path of the program as an argument to the Python interpreter. Such as follows:

Note that you must use the full path of the Python interpreter. If you want to simply type python.exe C:\Users\Username\Desktop\my_python_script.py you must add python.exe to your PATH environmental variable. To do this, checkout the adding Python to the PATH environment article..

Window’s python.exe vs pythonw.exe
Note that Windows comes with two Python executables – python.exe and pythonw.exe. If you want a terminal to pop-up when you run your script, use python.exe However if you don’t want any terminal pop-up, use pythonw.exe. pythonw.exe is typically used for GUI programs, where you only want to display your program, not the terminal.

Run a Python Script Under Mac, Linux, BSD, Unix, etc
On platforms like Mac, BSD or Linux (Unix) you can put a “shebang” line as first line of the program which indicates the location of the Python interpreter on the hard drive. It’s in the following format:

Note: Shebang Means

#!/usr/bin/env python “”” The first line in this file is the “shebang” line. When you execute a file from the shell, the shell tries to run the file using the command specified on the shebang line. The ! is called the “bang”. … The shebang line specifies exactly how to run a script.

A common shebang line used for the Python interpreter is as follows:

You must then make the script executable, using the following command:

Unlike Windows, the Python interpreter is typically already in the $PATH environmental variable, so adding it is un-necessary.

You can then run a program by invoking the Python interpreter manually as follows:

Python Execution with the Shell (Live Interpreter)
Assuming that you already have Python installed and running well (if you’re getting an error, see this post), open the terminal or console and type ‘python’ and hit the ‘Enter’ key. You will then be directed immediately to the Python live interpreter. Your screen will display a message something like: