How to Create a Batch File to Run Python Script

The idea is quite simple – create a batch file to run a Python script.

But why on earth would you want to do that?

It might be that you created a Python program, but the user of that program has no idea what is Python, and how to run a Python script.

As far as you know, the user might think that Python is just a snake…

So you decided to create a batch file that will run the Python program when the user clicks on that batch file.

Then, in this tutorial, I’ll show you the steps to create a batch file to run your Python script.

Before we dive into an example, here is the batch file structure that you can use to run the Python script:

 

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

 

In the next section, I’ll review an example to create a batch file that will open the Python GUI below. The same concepts would apply for any Python script that you’d like to run using a batch file.

 

Simple GUI in Python

Example of running a Python script using a batch file

(1) To start, type your code in Python. I used the below code to create a simple GUI with a single button to exit the application.

Alternatively, you may use any Python code that you’d like!

 

import tkinter as tk 

root= tk.Tk() 
   
canvas1 = tk.Canvas(root, width = 350, height = 250) 
canvas1.pack()
      
button1 = tk.Button (root, text='Exit Application', command=root.destroy) 
canvas1.create_window(170, 130, window=button1) 
    
root.mainloop()

 

(2) Save your python code (your python code should have the extension of ‘.py’).

In my case, I saved the Python code as ‘simple_gui’

Very creative, I know…

py file

(3) To create your batch file, open Notepad and then use the following code structure that we saw earlier:

 

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

 

You’ll need to adjust the code in two places:

  • “Path where your Python exe is stored\python.exe”
    In my case, the Python exe is located at: “C:\Program Files (x86)\Python36-32\python.exe”
  • “Path where your Python script is stored\script name.py”
    And in my case, the python script that I wish to run is located at:
    “C:\Users\Doron E\Desktop\Run Python\simple_gui.py”

This is how my batch script would look like in Notepad:

 

How to Create a Batch File to Run Python Script

 

Note that for Anaconda, you’ll need to look for the path where pythonw.exe is located (rather than python.exe).

(4) Save the Notepad with a ‘bat’ extension. Here, I named the batch file as Run_Script.bat:

 

Notepad bat

 

(5) Your batch file is now ready to go! Double-click on that batch file:

Batch file

(6)  Your Python script will run now…

For our example, the Python GUI will be displayed as follows:

 

Batch File to Run Python Script

 

That’s it! Mission accomplished!

Conclusion

We just saw how to create a batch file to run a Python script. There might be opposite cases, where you may want to run a batch file from Python itself.

In general, batch files are useful to work with, and in some cases are fun to create… but don’t tell anyone…

You may want to check the following tutorials on batch scripts: