Python Script Automation for Anancoda Distribution

If you are reading this, you already have a great python script which gets some job done and is ready to be set on auto refresh at a regular frequency. However, if your python installation has been through Anaconda Distribution, it can be a little different which not many articles around the web takes into account, specially when majority of the python users choose Anaconda distribution as a preferred choice.

Its a simple three step approach, faster and easier than you would have taught of:

Find out Python executable path

  1. Open Anconda promt
  2. Type command: where python
  3. Copy the path, this is the path where executable python program is installed which needs to be called to run the python script

Now, by default Anconda distribution doesn’t add path variables to your system. So the system doesn’t know where your installed libraries are stored etc. This requires activating Anaconda before python executable is called, to leverage entire pre-setup python environment (majorly libraries, dependencies etc. )

Create a .bat file

  1. Open a text file
  2. Type following details:
@echo on
call <path copied for python executable, trim the initial path till anaconda3>\anaconda3\Scripts\activate.bat
<path copied for python executable> "<path of python script>"
pause

  • activate.bat: activates the Anaconda environment (skip this if calling python installed outside of Anaconda distribution)
  • pause: prevent cmd to auto terminate after completion of script run

Create a task in Windows Task Scheduler

The interface for Task Scheduler is very intuitive and one can easily follow sequence of steps to set the script for auto trigger

Before Finish, check “Open Properties”, to change/control some critical elements involved in running the script. These settings will depend upon ones requirements and infrastructure, like server vs personal laptop, on which he is automating the script

Troubleshooting guide for few common mistakes

Run whether user is logged on or not

Start the task only in when computer is connected to AC power

Do not start a new instance” will not allow the Task Scheduler to run the scheduled program/script parallelly. In case cmd window is open (if pause command is used in the script as mentioned above) or previous occurrence of the task is still running.

Conclusion

The entire process of setting up script automation is easy and simple but highly impactful in one’s everyday work to remove dependency on people and deliver things on time without any additional overhead. However, one thing to keep in mind is, automating script also require programmers to generalize variables and use dynamic assignments avoiding hard coding to maximum extent. The people who practice these, also set themselves apart from the crowd, so be informed, be smart.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s