- How To Schedule Python Scripts to Automate Tasks using Windows Scheduler on Windows 10
- Step 1: Create your Python script.
- Step 2: Create Batch File to Run the Python Script
- Step 3: Schedule Python Script using Windows Scheduler
- How to Schedule Python Script using Windows Scheduler
- Steps to Schedule Python Script using Windows Scheduler
- 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
- Python Script Automation Using Task Scheduler (Windows)
- 2. Create an Action
- 3. Add the Python Executable File to the Program Script
- 4. Add the Path to Your Python Script in the Arguments
- 5. Trigger Your Script Execution
- Example Python Script
- Task Scheduler Python Script Automation FAQs
- ? Can Task Scheduler run a Python script?
- ? How to schedule a Python script with Task Scheduler?
- ⭐ Does Task Scheduler work when computer is sleeping?
- About Windows Task Scheduler
- Conclusion
- Execute Python Script on Schedule – Windows Task Scheduler
- Steps to use the Windows Task Scheduler
- 1. Open the task scheduler
- 2. Create a new task
- 3. Name the task
- 4. Create a new action for our script
- 5. Run python script
- 6. Create a trigger for the Script
- Conclusion
How To Schedule Python Scripts to Automate Tasks using Windows Scheduler on Windows 10
In this post, I will go over the steps necessary to schedule a Python script using Windows Scheduler. This can be particularly useful if you want to re-run a particular programme at a prescribed time, for example, for a web scraper.
For the purpose of this post I will further assume that you have Python set up and running on your machine. For everything else, please follow the step-by-step guide below:
Step 1: Create your Python script.
This should be self-explanatory. Let’s assume we want to run the following script called my.py , which does nothing but print today’s date:
#################### # Content of my.py # #################### import datetime # Get date and time todaysDate = datetime.datetime.today(); # Change display format (optional) todaysDate = todaysDate.strftime("%Y-%m-%d %H:%M:%S") # Print print(todaysDate)
Now save your script! In my case, I’ll simply save the Python script on my Desktop.
where should be replaced with your Windows username.
Step 2: Create Batch File to Run the Python Script
First, open Notepad and generate the following generic structure:
In our example, the batch file will read as follows:
"C:\Users\\AppData\Local\Programs\Python\Python38\python.exe" "C:\Users\\Desktop\my.py" pause
Note: In case you want your batch file to run Git to automatically add, commit and push to GitHub, you will need a couple of extra lines of code which I discuss in this blog post.
Next, save the file with the ‘bat’ file extension; we’ll call our file run_mypy.bat for now.
Finally, go ahead and check if the batch file triggers your Python script correctly. For this, you only have to double-click run_mypy.bat . If everything has worked out, a command prompt should open now containing the following pieces of information (including the date and daytime, which in this case is 2020-12-16 12:12:12):
C:\Users\\Desktop>"C:\Users\\AppData\Local\Programs\Python\Python38\python.exe" "C:\Users\\Desktop\my.py" 2020-12-16 12:12:12 C:\Users\\Desktop>pause Press any key to continue . . .
Step 3: Schedule Python Script using Windows Scheduler
- Open Control Panel and search for Administrative Tools .
- Select Task Scheduler . There, select option Create Basic Task. in the right panel.
- Give your Task a name (in this case we’ll name the task ‘Run mypy’), and now define how often and when you want the task to be triggered. This step is rather self-explanatory.
- Once you have configured the trigger for your job to your satisfaction, click next and define the Action which you want the scheduled task to perform. In our case we want to Start a program .
- Click Next and then on Browse . Then navigate to where your batch file sits. In our case C:\Users\\Desktop\run_mypy.bat .
- IMPORTANT: If you are planning on saving/appending data that you obtain from running the Python script, make sure you also provide the location of your application folder in Start in (optional) . This allows the Scheduler to access all of the relevant elements (files, folders etc.) of your project. In our case, this would be C:\Users\\Desktop\ .
- Click Next again and the final screen will provide a summary of the new task you have just created. You can always navigate back and change its configuration. Once you’re happy with your result click Finish and you’re done!
- To check if the task you created works, access the Task Scheduler Library (left panel in Task Scheduler), select the task from the list of created tasks (middle panel), right-click and then click on Run . A terminal window should open and return the same result as when double-clicking run_mypy.bat .
This last step completes this tutorial. If you want to learn more about Windows Task Scheduler, check out the online documentation.
How to Schedule Python Script using Windows Scheduler
In this guide, you’ll see the steps to schedule a Python script using Windows Scheduler.
Steps to Schedule Python Script using 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:
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
- The path where the Python exe is storedis:
“C:\Users\Ron\AppData\Local\Programs\Python\Python39\python.exe” - The Path where the Python script is storedis (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:
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:
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.
Python Script Automation Using Task Scheduler (Windows)
This post will show you how to schedule a Python Script execution using Windows Task Scheduler. This will help you automate tasks using Python on Windows.
Before we can cover all that, we need to learn how to use Windows Task Scheduler.
This will open the Windows Task Scheduler GUI.
Go to Actions > Create Task…
2. Create an Action
Go to Actions > New
3. Add the Python Executable File to the Program Script
Find the Python Path using where python in the command line.
From the command prompt copy the script to use in the action.
C:\yourpath\python.exe
or in my case
C:\Users\j-c.chouinard\AppData\Local\Continuum\anaconda3\python.exe
In Program/Script, add the path that you have copied from the command line.
4. Add the Path to Your Python Script in the Arguments
Go to the folder where your Python script is located. Right-click on the file and select Copy as path .
If you have a file located at this location.
In the «Add arguments (optional) ” box, you will add the name of your python file.
In the «Start in (optional)» box, you will add the location of your python file.
Note: Alternatively, you could create a batch file combining your Python script and Python executable file in a .bat file.
5. Trigger Your Script Execution
Choose the repetition that you want. Here you can schedule python scripts to run daily, weekly, monthly or just one time.
Once, you have set this up, your trigger is now active and your python script will run automatically every day.
Whether you decide to repeat the task every week or every hour, you may use the task scheduler wizard.
This is the best way to schedule a function to run at a specific time of the day without using CRON job. However, there are alternatives to run it from within your code using Advanced Python Scheduler, but I don’t like that option as it requires the code to keep running.
Example Python Script
from datetime import datetime import os def write_file(filename, data): if os.path.isfile(filename): with open(filename, 'a') as f: f.write('\n' + data) else: with open(filename, 'w') as f: f.write(data) def print_time(): now = datetime.now() current_time = now.strftime("%H:%M:%S") data = "Current Time = " + current_time return data write_file('test.txt' , print_time())
Task Scheduler Python Script Automation FAQs
? Can Task Scheduler run a Python script?
Yes, you can execute a Python script with Windows Task Scheduler. If your script works using the command prompt, you can schedule your script to run at a specific time and date.
? How to schedule a Python script with Task Scheduler?
To schedule a Python script with Task scheduler, create an action and add the path to your Python executable file, add the path to the script in the “Start in” box and add the name of the Python file ase an argument. Then, create a trigger to schedule the execution of your script.
⭐ Does Task Scheduler work when computer is sleeping?
It does not work by default, but you could enable it in the conditions tab. To run your script while your computer is sleeping, just select the option: “Wake the computer to run this task”.
About Windows Task Scheduler
Conclusion
That’s it, you now know how to run a Python script automatically using Windows Task Scheduler.
Execute Python Script on Schedule – Windows Task Scheduler
Sometimes we want our script to run at a specific time or a specific number of times. We can automate this task so we don’t need to run the script manually. Windows provides a software allied task scheduler that will run our script at a given time.
Limitations:
- Works only on Microsoft Windows operating system
- Your Computer needs to be switched on at the scheduled time
- Doesn’t not imitate Server like – specific time execution. So in order to run a code at any time without the hassle of doing it self (switching the computer on) – you need to do using a Server (execution of script on server)
Steps to use the Windows Task Scheduler
Let’s get right into the steps to schedule the execution of Python scripts right away. Follow through and let us know if you have any questions later! We’ll make use of the Windows task scheduler for easier setup.
1. Open the task scheduler
Open the task scheduler application on your computer by searching for Task Scheduler in the start menu.
2. Create a new task
Next, create a task in the task scheduler by right-clicking on the Task Scheduler (Local).
3. Name the task
Add a name so you can identify it later.
4. Create a new action for our script
Switch to the “Actions” tab and add a new action. This is where the real stuff happens.
5. Run python script
To run the python script automatically using Windows scheduler we need to follow the below steps in our code:
- We need the path of our python installation which we can find by running the following python code.
import sys print(sys.executable)
- We’ll paste this location in box number 1 of our Windows Scheduler action
- In the box number 2 we’ll pass the name of the script file (python file)
Ex: python_sample_file.py or a.py - In box number 3 we’ll pass the path of our Python executable (python file)
6. Create a trigger for the Script
A trigger means an event that causes our script to run. For example, we can specify a time on which our script will get executed. It provides options like what day, what time, how many times, for how long our script should get executed.
Hence, you’ll need to enter all the required details of it.
For our program, we’ll set a specific time that will trigger our following script to execute:
print("Hi, this is scheduled message")
On successful creation of our Windows Task Scheduler and upon the trigger of the python script file, the script is executed and the output is displayed as follows in the Windows Command Prompt:
Conclusion
Hope you have learned well how to run your python script file at any desired time. Scheduling a task, which is quite useful in attaining automation of your script file.