Windows OS

How to Run Python Scripts on Windows

Python is a general-purpose programming language primarily used to design websites, automate tasks and services, and create web APIs. The Python script is a python file with a “.py” extension. It contains multiple commands to import packages, libraries, and modules. The script file can be runnable on a Python interpreter or shell. However, we can also use the Command Prompt for this purpose.

In this article, we will review the methods used to install Python and execute Python scripts on Windows. So, let’s start!

Install Python on Windows

To run the Python Script, it is required to install Python first on Windows. Go through the below-provided steps to install the latest version of Python on Windows.

Step 1: Download Python Installer

In the first step, visit the below-provided link to download the latest Python installer on Windows:

https://www.python.org/downloads/

Step 2: Execute Python Installer

Open the “Downloads” folder and execute the Python installer file:

Step 3: Install Python

Mark the highlighted checkboxes to grant permissions. Then, hit the “Install Now” to install Python on Windows:

You can see that from the below-given output, we have successfully installed Python on Windows:

Let’s check out the method to create the new Python Script file on Windows.

Create Python Script on Windows

To make a new simple Python Script file on Windows, follow the below-given procedure.

Step 1: Create Simple Text File

Firstly, create a simple text file named “PythonProgram.txt”:

Step 2: Change File Extension to “.py”

Next, rename the file and replace the “.txt” extension with “.py”:

The “Rename” alert message will appear on the screen for confirmation. Allow the added changes by hitting the “Yes” button:

Step 3: Open File

Right-click on the file, choose the “open with” option, and then select “Notepad” to open the Python script in Notepad:

Step 4: Add Python Code

Paste the below-provided code into the Python script and press “CTRL+S” to save changes:

#!/usr/bin/env python3
print('Hello! This is Python Program')

Now, let’s go through the method of running the created Python script on Windows.

Run Python Scripts on Windows

Python scripts can be run on a Python interpreter or Windows Command Prompt. To run the Python script on Windows Command Prompt, follow the listed steps.

Step 1: Open Command Prompt

Press “Window+R” to open the “Run” box and type “cmd” in the drop-down menu to open Command Prompt:

Step 2: Move to Python Script Directory

Execute the “cd” command and also define a path where the Python script is placed:

>cd C:\Users\anuma\OneDrive\Documents

Step 3: Run Python Script

Run the Python script file using “python” command and specify script name as follows:

>python PythonProgram.py

The below-given output indicates that we have successfully run the created Python script file on our Windows system:

We have elaborated the easiest method to install Python and how to create and run Python script on Windows.

Conclusion

To run the Python script on Windows, it is required to install Python first. For this purpose, visit the Python official website, download the Python installer, and execute it to install Python on Windows. Next, create a simple text file and replace the file extension “.txt” with “.py”. To run the Python script in Windows Command Prompt, utilize the “python [filename.py]” command. We have offered the easiest solution to install Python and run Python script files on Windows.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.