Scheduling Python scripts with Windows Task Scheduler allows you to automate tasks and execute scripts at specific times or intervals. This can be useful for tasks like data processing, backups, or sending email notifications.
Creating a Batch File
Create a batch file (.bat) using a text editor.
For virtualized environments, activate the virtual environment, run the script, and deactivate the virtual environment.
call C:\path_of_your_project\.venv\Scripts\activate.bat
C:\path_of_your_project\.venv\Scripts\python.exe C:\path_of_your_project\main.py
call deactivate
- For non-virtualized environments, specify the Python executable path and script path.
C:\Users\emeer\AppData\Local\Programs\Python\Python312\python.exe
C:\path_of_your_project\main.py
Scheduling the Batch File
Open the Task Scheduler.
Create a new basic task.
Provide a name and description for the task.
In the "Triggers" tab, choose when to run the task (e.g., daily, weekly, or at startup).
In the "Actions" tab, select "Start a program" and browse to the batch file created earlier.
In the "Conditions" tab, set any conditions for running the task (e.g., network connection or idle status).
Review the task settings and click "Finish" to save the scheduled task.
Now, the Python script will run automatically according to the specified schedule.