In this tutorial, I’ll show you the steps to schedule a Python script using Windows Scheduler. This type of exercise is especially useful when you want to run a Python script at prescribed times.
To show you how the process works, I’ll use a simple example that will display ‘Hello World!’ each day at 6am.
Hope it’s not too early…
Steps to Schedule Python Script using Windows Scheduler
Step-1: Prepare the Python Script
In our example, I’ll use the tkinter module to display the label of ‘Hello World!.’
Alternatively, you may use any Python script that you’d like to schedule.
Here is the Python script that I used:
import tkinter as tk root= tk.Tk() canvas1 = tk.Canvas(root, width = 300, height = 300) canvas1.pack() label1 = tk.Label(root, text='Hello World!') canvas1.create_window(150, 150, window=label1) root.mainloop()
Step-2: Save the Python Script
Once you’re done writing the script, save it as a Python file (which should have the .py file type):
In my case, I saved the Python script on my Desktop, under this path:
Step-3: Create Batch File to Run the Python Script
Next, you’ll need to create a batch file to run the Python script.
To start, open Notepad, and then apply the following generic structure:
"Path where your Python exe is stored\python.exe" "Path where your Python script is stored\script name.py" pause
In my case:
- Path where my Python exe is stored\python.exe is:
- Path where my Python script is stored\script name.py is (see step-2):
You’ll need to adjust those paths to fit your instances.
Here is a glance of how my Notepad would look like after adding the paths:
Finally, save the Notepad with your file name and the “bat” extension:
For example, I decided to save the Notepad on my Desktop as:
Once I saved the Notepad, a new batch file (called Run_Python_Script) got created on my desktop:
This batch file will run the Python script when double-clicking on it:
In the final step below, you’ll see how to schedule that batch file to execute the Python Script using the Windows Scheduler.
Step-4: Schedule the Python Script using Windows Scheduler
For this step, I’m going to use Windows 10 to execute the Python Script via the Windows Scheduler. Similar principles would apply when using previous versions of Windows.
First, open the Control Panel and then click on the Administrative Tools:
Next, double-click on the Task Scheduler, and then choose the option to ‘Create Basic Task…’
Type a name for your task (you can also type a description if needed), and then press Next.
Here, I named the task as: Run Hello World
Next, I chose to start the task ‘Daily’ since we wish to run the Python script daily at 6am:
The action will then recur everyday at 6am, staring from 2019-04-01. You can adjust those timing parameters to suit your needs.
Select, Start a program, and then press Next:
Next, use the Browse button to find the batch file that runs the Python script. In my case, I placed the Run_Python_Script batch file on my Desktop:
Finally, click on Finish, and you should be good to go:
From this point onward, you’ll be greeted with ‘Hello World!’ everyday at 6am:
Please note that there is more than one way to execute Python Script using the Windows Scheduler. I chose to create a batch file to run the Python script.
This is especially useful for users who are not familiar with Python, but want to run the script without even launching the python application. At any given point in time, in addition to prescribed times, the user can simply double-click on the batch file and get the output from Python.
Finally, you my want to visit the task scheduler documentations to find out more about scheduling tasks.