How To Run Python Script [Complete Guide] 2021

Have you started coding or programming in the most well know high-level programming language Python and You are searching for how to run Python Script? It’s Simple, if you are using Python Then Press F5.

So, as you would already know that the Python script comes with an extension of “.py” or sometimes it can be “.pyw”. Note that to run the Python Script on Windows, You need to confirm if you have Python Interpreter installed first.

If you don’t have python installed on your system then follow this guide to install python.

How to Check if Python is Installed?

To check if you have Python installed[1] on your system then go to the command prompt and enter the “python -V”. This command will print the latest python installed version available on your system.

How To Run Python Script

There may be a chance that your Python is not set on Environment Variables to be accessed from anywhere from the command prompt. Hence in that case you should go to the required Python Installed folder installed in the C drive and then type the above command.

What is Modules, Script and Code in Python?

When it comes to computers, code refers to the language used to translate between human languages and the ‘words’ that computers understand. Also known as a collection of statements that collectively make up a program. 

The script is a term used in programming to describe a file having a logical sequence of orders or a batch file. This is usually just a basic text file with some code in it. Interpreters are used to executing scripts in a logical order. A script is an informal name for a top-level program file that contains Python code and is intended to be executed by the user.

A plain text file, on the other hand, that includes Python code that is intended to be imported and utilized from another Python file is referred to as a module. A module and a script are fundamentally different in that modules are built to be imported, whereas scripts are built to be executed immediately.

What matters most in either situation is that you understand how to run the Python code that you write and include in your modules and scripts. In Python, a module is an object that has random properties and to which you can attach and reference them.

How to Run the Python Script: 4 Ways

Once you have confirmed using the above method that you have Python installed on your system. You can use the below 3 methods to run your python script in Windows easily.

Method 1: Run Python Script Using Command Line

The first method to run the Python script is to paste the python script in the folder where you want it to run. Open the command prompt and then navigate to that folder.

Once you have navigated to that folder, you can type the command shown in the image. Let me write a simple python code and try to show you how can you run it.

#Example Code to Learn How to Run Python Script
name = "Coduber.com"
print("This website is "+name)

Save the above code as name.py. We have saved it with that name kept it in the Test folder in the C drive. Then we navigated to the test folder using the command prompt and type “python name.py” and press enter. This will print the line present in the print bracket in the above code.

How To a Run Python Script

Method 2: Run Python Script Using Interactive Python

Another method to run the python script or code is using interactive mode. Suppose you have the same code as mentioned above and you want to run it in interactive mode.

Then you open the command prompt and type the command “python” and press enter. Once you have pressed enter you will enter into interactive python-mode. In that, you can enter the above code line by line to get the result. Let me show you by example.

How To Run Python Script

Method 3: Run Python Script Using Python IDE

After you have installed the python. You will see that Python IDLE is installed as well. Python IDLE is basically a text editor and GUI to write, save and execute the Python scripts.

To open just enter “IDLE” in your run command and the python Shell window will open up. In Python Shell go to the File menu and select a new file.

How To Run Python Script

After selecting a new file. A text editor supporting python will open. In that text editor window type the below code and then press button “F5” on your keyboard.

If you haven’t saved the file you will be prompted to save the file first. So, save the file and press the F5 button. Then you will see the output of your program on the Python Shell window.

How To Run Python Script

Method 4: Popular Text Editor (VS Code)

If you are someone who is using VS Code to write the Python code, then running python code VS Code is also very simple. First, just make sure that the Python extension is installed on the VS Code text editor.

Once you start writing your python program in VS Code, if the python interpreter is not installed as an extension in the editor you will be prompted to install it. Press install and wait once the installation is complete.

How To Run Python Script

Now open the VS Code editor and write a small hello world program. Once you have written the program and saved it, press “F5” or the run button present in the text editor.

How To Run Python Script

Then you will be able to see the program output on the terminal below the text editor.

That’s it those are the above 4 methods to run python scripts anywhere. If you liked our post let us know in the comment section. Please follow us on Facebook and Twitter.

Further Read:

  1. List vs Dictionary In Python Complete Comparision
Share your love

One comment

Leave a Reply