Use a python script to trigger other python scripts
Did you know that you can write a simple python function 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 -
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.
To trigger above script, you just need to write a python script which calls the function in this way -
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.