Schedule a Python script to run automatically on a Windows Desktop/Azure VM

Author:Murphy  |  View: 21825  |  Time: 2025-03-23 19:43:48
Photo by Ales Nesetril on Unsplash

Overview

Let's assume that you have a couple of Python Scripts that you have developed on the Anaconda environment on a Windows Desktop/Azure VM. Let's say that you would like to execute these scripts on a schedule. Here, we discuss a way to automate the script run. Since you are using the Anaconda environment, you may have installed all the python package dependencies in Anaconda.

The Process

Below is a schematic showing the process involved in the automation. As you can see below, we will be using Windows Task Scheduler for this.

Schematic showing the overall process (by the author)

Let's go through the steps –

Step 1 – Writing a Batch Script

The batch script does two things –

  1. It activates the Anaconda Environment
  2. Once it activates the environment, it executes the python script in that environment.

Before we start writing the batch script, let's identify the Anaconda Environment we plan to use –

From the ‘Start' Menu, open ‘Anaconda Prompt' and type the following command –

conda info --envs

This command will list all the Anaconda environments available on your machine. Make a note of the name of the Anaconda environment you would like to use.

Now, let's proceed with the batch script.

To activate the Anaconda environment, the following can be added to the batch script –

@CALL "C:ProgramDataAnaconda3Scriptsactivate.bat" base

The above is assuming that your Anaconda installation resides in ‘C:ProgramDataAnaconda3'; and that ‘base' is the Anaconda environment you would like to use.

To execute the python script, the following can be added to the batch script –

python C:UsersMyscriptsAutomationScript_Name.py

The above is assuming that the python script resides at C:UsersMyscriptsAutomation and has a name of ‘Script_Name.py'.

Putting it all together, the batch script would look like the below –

@CALL "C:ProgramDataAnaconda3Scriptsactivate.bat" base
python C:UsersMyscriptsAutomationScript_Name.py

You can use any text editor (e.g. Notepad or Notepad++) to create this file. Save it with a .bat extension (e.g. ‘mybatchfile.bat').

Step 2 – Scheduling the Execution of the script in an automated fashion

Launch ‘Windows Task Scheduler' from the Start Menu.

Click on ‘Create Task' –

Give a name for the task that indicates the function of the script. Ensure that you select ‘Run whether the user is logged on or not' in case you would like the script to run even if the user is not logged in. Also, select ‘Run with highest privileges to ensure that there are no permission/privilege issues affecting the script execution.

Now, click on the ‘Triggers' tab and click on ‘New'.

Select the frequency of the schedule and/or any random delay that you wish to add.

Next, go to the ‘Actions' tab and click on ‘New'.

Select the Action ‘Start a program'. Then, provide the location of the batch file that you created (in Step 1) in the ‘Program/script' text box. Click on ‘OK'.

Do take a look at the ‘Conditions' and the ‘Settings' tabs if you would like to further control the automation runs.

Concluding Remarks

You might wonder why we cannot use Task Scheduler directly to run the Python script. Since we have all our package dependencies installed using Anaconda, why not just use that rather than installing all packages again to run the script via Task Scheduler? This is why we use the batch script to activate the Anaconda environment and then run the Python script there. Happy Learning!

Tags: Automate Python Script Data Refresh Scheduled Tasks Task Scheduler

Comment