Windows Scheduler is a task scheduler in Microsoft Windows that launches computer programs or scripts at pre-defined times or after specified time intervals. This feature was introduced in Microsoft Plus! for Windows 95 as System Agent, and its core component is an eponymous Windows service.
In this tutorial, we will learn how to schedule a Python script to run automatically on your Windows computer using the Task Scheduler. This tutorial covers the steps involved in creating a task that will run your Python script at a specific time or interval.
For example, let’s suppose that the goal is to display ‘Hello World!’ each day at 6am.
Here is the Python script to be used for our example (you may use another Python script based on your needs):
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()
Once you’re done writing the script, save it as a Python file (where the file extension is .py):
For instance, let’s save the file as hello_world.py under the following path:
C:\Users\Ron\Desktop\hello_world.py
To start, open Notepad, and then use the following template:
"Path where your Python exe is stored\python.exe" "Path where your Python script is stored\script name.py"
pause
For our example:
Here are the paths in the Notepad (you’ll need to adjust those paths to fit your instances):
"C:\Users\Ron\AppData\Local\Programs\Python\Python39\python.exe"
"C:\Users\Ron\Desktop\hello_world.py"
pause
Finally, save the Notepad with your file name and the “.bat” file extension:
file_name.bat
For example, let’s save the Notepad as:
run_python_script.bat
After you saved the Notepad, a new batch file (called run_python_script) would be created at the specified location:
C:\Users\Ron\Desktop\run_python_script.bat
In order to schedule the Python script using the Windows Scheduler:
C:\Users\Ron\Desktop\run_python_script.bat
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.
Thanks for reading !!!