Schedule Python Script in Windows Scheduler

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.

  • Step 1: Prepare the Python Script
  • Step 2: Save the Python Script
  • Step 3: Create a Batch File to Run the Python Script
  • Step 4: Schedule the Python Script using the Windows Scheduler


 

Step 1: Prepare the Python Script

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()

Step 2: Save the Python Script

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

Step 3: Create a Batch File to Run the Python Script

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:

  • The path where the Python exe is stored is:
    “C:\Users\Ron\AppData\Local\Programs\Python\Python39\python.exe”
  • The Path where the Python script is stored is (see step-2):
    “C:\Users\Ron\Desktop\hello_world.py”

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

Step 4: Schedule the Python Script using the Windows Scheduler

In order to schedule the Python script using the Windows Scheduler:

  • Open the Windows Control Panel and then click on the Administrative Tools
  • 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. For instance, let’s name the task as: Run Hello World
  • Choose to start the task ‘Daily‘ since we wish to run the Python script daily at 6am. Also specify the start date and time (6am for our example)
  • Select, Start a program, and then press Next
  • Use the Browse button to find the batch file (run_python_script.bat) that runs the Python script. In our case:
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 !!!

#python 

Schedule Python Script in Windows Scheduler
3.85 GEEK