How to run/execute python scripts?


A python program is a script saved with a .py file extension. Similar to other scripting languages e.g PHP, NodeJs, etc, it also executes line by line by the python interpreter.

Before learning how to execute a program, first, you may need to download and install python on the machine you are working on.

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

Here is a basic hello program.  Only has a print function.  Open a  text editor (e.g Notepad on Windows and VIM on Linux), copies the lines, and save the file with the .py extension.  From the py extension, the interpreter knows that the input file is a python script.

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

How to run?


On Windows –  You can run the script from the 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?

py --version

Output -> Python 3.8.5

Run the script-

py HelloWorld.py

Output -> 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

Output -> Python 3.8.5

Run the script-

python HelloWorld.py

Output -> Hello World, I am here