Automate Python Script In Windows

Sharing is caring!

Last Updated on October 28, 2021 by Jay

We are going to take our Amazon price notification bot to another level by automating our Python script, meaning we don’t have to manually run it anymore – note this tutorial covers the method on Windows only. Our bot should run by itself every day at a set time, and only reports to us when it detects a price drop for an item.

Automate Python Scripts In Windows

Automate Python Script Using Windows Task Scheduler

In the Windows search bar, type “Task Scheduler”, open the program. Then from the right-hand side panel -> Create Task… Give your task a name.

Create A New Task In Windows Task Scheduler

Go to the “Tiggers” tab, change the settings to Daily, and make sure recur every 1 day since we want the bot to run every day. Also, make sure the Start time is in the past, otherwise, it won’t run until that time comes. We don’t need to touch other settings for our simple bot. Click on OK to save the settings. We should see a trigger added on the screen.

Create A New Task In Windows Task Scheduler

Go to the next tab Actions. Click on New… Then we can Browse to select our Python script. OK to save.

Test The Scheduled Job

To test the scheduled job, we’ll set the Start time to be 1 minute after the current time, whatever it is as you are reading this, then save the changes. Make sure that you finish saving the scheduled job before the trigger time comes.

Watch for it and as soon as your computer clock hits the trigger time, a black screen should pop up. Do not worry as that is our bot starts to work! When the black screen goes away (should be fast), check your email, and you should have received a notification from the bot.

Run Scheduled Task WIth A Virtual Environment

Given that sometimes we might use a virtual environment for projects, we need a way for the scheduler to activate the virtual environment before running a script.

To do that, we need to write a very simple batch script. In the code example below, the first part is just the address for the activate.bat file, followed by two &&, then followed by the address of the Python script.

The && here means that run the second command only if the first command runs successfully. Essentially this allows us to activate the virtual environment first, then run the script using that virtual environment.

C:\Users\jay\Desktop\PythonInOffice\amazon_price_alert_bot\venv\Scripts\activate.bat && python C:\Users\jay\Desktop\PythonInOffice\amazon_price_alert_bot\

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *