How to run or execute Python scripts?


A Python program is a script saved with a .py file extension. Like other scripting languages, e.g., PHP, NodeJs, etc., it executes line by line using the Python interpreter.

Before start learning how to execute a Python script, you may need to download and install Python on the machine you are working on if it is not there.

In this tutorial, you will learn how to execute a Python script on Windows and Linux operating systems.

Here is a basic Hello program that has a print function only. To create the program, open a  text editor (e.g., Notepad on Windows or VIM on Linux), copies the line below, and save the file with a .py extension. By looking at the py extension, the interpreter knows that the input file is a Python script.

print('Hello World, I am here');

How to run the Python script On Windows and Linux?


How to run On Windows?

You can run the script from the cmd terminal. To open a terminal in Windows, you need to press the Windows + r key and then type cmd or command.

Once opened, check if Python is installed by using the following command.

py --version
Python 3.8.5

Run the script with the Python interpreter on Windows.

py HelloWorld.py
 Hello World, I am here

How to execute on Linux?

To open a Linux terminal, you need to do ssh to the server or can open the terminal by doing control + t.

Once opened, check if Python is installed.

python --version
Python 3.8.5

Run the script with the Python interpreter on Linux.

python HelloWorld.py
Hello World, I am here