In this tutorial, I’ll show you the steps to execute 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 execute 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 whatever Python script that you’d like to schedule.
Here is the Python script:
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 are done writing the script, save it as a Python file (which should have the .py file format):
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 apply the following generic structure:
@echo off "Path where your Python exe is stored\python.exe" "Path where your Python script is stored\script name.py"
In my case:
- “Path where your Python exe is stored\python.exe” is:
“C:\Program Files (x86)\Python37-32\python.exe”
- “Path where your Python script is stored\script name.py” is (see step-2): “C:\Users\Doron E\Desktop\Hello_World.py”
You’ll need to adjust those paths to your instances.
Here is a glance of how my Notepad would look like after adding the paths:
Finally, save the Notepad as follows:
Your file name + ‘.bat’ extension
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 upon a double-click:
In the final step below, we’ll see how to schedule that batch file to execute the Python Script using Windows Scheduler.
Step-4: Execute 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, 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 as needed), and then press Next.
Here, I chose to name the task as: ‘Run Hello World’
In our example, I chose to start the task ‘Daily’ since we are interested to run the Python script daily at 6am:
The action will then recur everyday at 6am, staring from 07-Oct-2018. You can adjust those timing parameters to suit your needs.
Choose, ‘Start a program’, and then press Next:
You may then 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:
Press 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 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.