Use a python script to trigger other python scripts

But why did I think of it in the first place when options like shell script are available to trigger scripts?

Here are 3 reasons:

  • I wanted to dynamically pass arguments to my python scripts which were based on data derived from other python script.
    Example - For our dashboard creation scripts, we pulled all the missing dates in our data tables through SQL query and passed the dates as an argument to run the script for those specific dates only thereby backfilling the missing data on the go.
  • I wanted to trigger scripts in a particular sequence in case the scripts were required to strictly follow the order to give the desired output.
  • I wanted to trigger a mail to myself or my teammates in case those scripts shut down due to an error.

With these requirements in mind, I created a python function which looks like this -

trigger function

Note - Remember to declare mail_func in line number 66 if you want to receive mails as per the mail service you may be using

Now let’s create a small random code in which we will take 2 arguments - a country and a city and just print them.
Let's name it sample.py.

sample script

To trigger above script, you just need to write a python script which calls the function in this way -

trigger script

If you don't want to set up complex & costly cron/task management utilities for your machine as an analyst, this method of managing scripts will be a blessing for you as you will be able to write smart workflows within python itself. Plus, with minor changes, this method can be extended to trigger Rscripts or other scripts also.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Lavesh Dalmiya

Lavesh Dalmiya

7 Followers

I am a Product Manager at LohonoStays. I transitioned to this role after spending 4.5 years in Analytics to ship products & increase revenue based on data!