Techno Blender
Digitally Yours.

Schedule a Python script to run automatically on a Windows Desktop/Azure VM | by Subha Ganapathi | Feb, 2023

0 460


Automation
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:\ProgramData\Anaconda3\Scripts\activate.bat" base

The above is assuming that your Anaconda installation resides in ‘C:\ProgramData\Anaconda3’; 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:\Users\Myscripts\Automation\Script_Name.py

The above is assuming that the python script resides at C:\Users\Myscripts\Automation and has a name of ‘Script_Name.py’.

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

@CALL "C:\ProgramData\Anaconda3\Scripts\activate.bat" base
python C:\Users\Myscripts\Automation\Script_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.

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!


Automation
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:\ProgramData\Anaconda3\Scripts\activate.bat" base

The above is assuming that your Anaconda installation resides in ‘C:\ProgramData\Anaconda3’; 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:\Users\Myscripts\Automation\Script_Name.py

The above is assuming that the python script resides at C:\Users\Myscripts\Automation and has a name of ‘Script_Name.py’.

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

@CALL "C:\ProgramData\Anaconda3\Scripts\activate.bat" base
python C:\Users\Myscripts\Automation\Script_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.

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!

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.

Leave a comment