How To Execute Python Scripts In Command Prompt by manish sharma

Execute Python Scripts In Command Prompt

In the previous tutorial we learnt how to create and execute a Python script using IDLE. But there are instances when we have to execute the scripts using command prompt. Thus, in this tutorial we will learn how to execute Python scripts using command prompt in windows 10.

Before starting anything let me quickly explain the setup we will be using for the demonstration. I have created a directory with the name “Python Tutorials” in my D drive. In that directory I have created a script with name “Tut1.py”. This script contains a code which on successful execution displays the current version of Python installed on my system.

How To Execute Python Scripts In Command Prompt by manish sharma

Now let’s move ahead and learn how to execute this script using command prompt in windows 10.

There are two ways of doing it:

  1. In your command prompt, navigate to the directory where your scripts are saved and then execute them using Python interpreter. Or
  2. Invoke the Python interpreter with the location of your script. The interpreter will use the location path to find the script and then execute it.

Let’s try to execute our script “Tut1.py” using both the above mentioned ways. Let’s start with the first way which is navigating to the directory.

Navigating To the Directory

Step 1: Navigating to the script location.

In order to execute your script using command prompt you first need to locate the script in window’s file system and then navigate your command prompt to its location. For example in my case the script is located at “D:/Python Tutorials/Tut1.py” so I need to navigate my command prompt to this location.

Command prompt tips

  • To change the drive in command prompt simply write the drive letter along with colon and hit enter. For example to change from C to D drive simply write D: and hit enter.
  • To change the current directory in command prompt we use “cd” command. For example to get into the directory “Python Tutorial” simply write “cd Python Tutorials”

How To Execute Python Scripts In Command Prompt by manish sharma

Step 2: Invoke the Python interpreter to run the script.

Python’s interpreter can be invoked simply by writing keyword “python” on command prompt. In order to execute a script we need to accompany the keyword “python” with the name of the script enclosed inside a pair of double quotes.  Like this:

D:\Python Tutorials>python "tut1.py"

Info: Always take care of two things.

  1. Always enclose the name of the script inside a pair of double quotes and
  2. Add *.py file extension at the end of the name of the script.

These two rules are mandatory. You have to follow this otherwise you will get an error.

Invoke the Python interpreter with the script location

This method of executing Python script is easier than the previous one. In this method we invoke Python’s interpreter with the location of the script. Let’s once again run the same Python script “tut1.py”.

The location path of the “tut1.py” script is D:\Python Tutorials\tut1.py (this location could be different in your case.)  In order to run this script we have to specify this location followed by the keyword Python in the command prompt. Like this

C:\>python "d:\Python Tutorials\tut1.py"

On pressing the enter key you will see the result if there is no error but if there is any then you will see the error stack on your screen.

The rules for running the script will remain the same.

  1. Always enclose the location of the script inside a pair of double quotes and
  2. Add *.py file extension at the end of the name of the script.

So these are the two different ways in which you can execute python scripts in command prompt in windows 10. Hope you will find this tutorial helpful. If so then I want you to share this blog on your social media. I will be really grateful. Thanks & have a great day!