How to Run a Python Script in a Batch File

Published on Aug. 22, 2023, 12:12 p.m.

In this guide, you’ll see the complete steps to create a batch file.

Here is the batch file template that you can use to run your Python script:

@echo off
"Path where your Python exe is stored\python.exe" "Path where your Python script is stored\script name.py"
pause

Steps to create a Batch File to Run a script from the above.

Step 1: create the script :

countdown = 10

while countdown > 0:
    print ('CountDown = ', countdown)
    countdown = countdown - 1

Step 2: Save

save your Python script (your script should have the extension of ‘.py).

Step 3: Create the Batch File

@echo off
"Path where your Python exe is stored\python.exe" "Path where your Python script is stored\script name.py"
pause

You’ll need to adjust the syntax.

@echo off
"C:\Users\Ron\AppData\Local\Programs\Python\Python39\python.exe" "C:\Users\Ron\Desktop\Test\countdown.py"
pause

Save the file with a ‘.bat‘ extension.

A new batch file, called “Run_Script.bat,” will be created at your specified location.

Step 4: Run

Finally, click on the file in order to run the Python script.

CountDown = 10
CountDown =  9
CountDown =  8
CountDown =  7
CountDown =  6
CountDown =  5
CountDown =  4
CountDown =  3
CountDown =  2
CountDown =  1