Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linking multiple python scripts to run one after another

I have three python scripts. One gathers data from database(data_for_report.py), another generates report from that data and creaters .xlsx file(report_gen.py) and the last one modifies the style of that excel file(excel_style.py).

Now all three files are in the same directory and what I do now is simply execute scripts one after another in the interpreter to get the report. I want to make everything work with one click so people who need this report could do it themselves. I thought of creating an exe with pyinstaller, but I can not think of a way to link my scripts together so that when data_for_report.py ends its job report_gen.py is started and so on.

I tried to put

subprocess.call("report_gen.py", shell=True)

at the end of the first script, but nothing happens, I just get this:

Out[2]: 1

How could I do this?

like image 838
milka1117 Avatar asked Jan 02 '23 01:01

milka1117


2 Answers

Actually, This problem can be solved by using batch programming. Your python files will run in batches i.e. one file after the other. I am assuming your all three python files resides in folder ReportGenerator with Path as C:\ReportGenerator so adjust accordingly the PATH as of your system (Please care for \ and / in PATH of folder having the python files).

Your files are which need to be executed:

data_for_report.py
report_gen.py
excel_style.py

Now open a Notepad file and write the below lines.

cd C:/ReportGenerator
python data_for_report.py
python report_gen.py
python excel_style.py
PAUSE

Now save this file with file_Name.bat anywhere u want in system and remember it. After saving the batch file icon will form on saving. Now Open window command prompt and just drag this batch file to window command prompt.

like image 59
Rink16 Avatar answered Jan 03 '23 15:01

Rink16


Why not encapsulate all the logic for each script in a function, make a new file which imports all the 3 functions, and then run that script.

So if the scripts are

data_for_report.py

def f1():
  ...

report_gen.py

def f2():
  ...

excel_style.py

def f3():
  ...

Then the final script which you will run is :

from data_for_report import f1
from report_gen import f2
from excel_style import f3

f1()
f2()
f3()
like image 36
Devesh Kumar Singh Avatar answered Jan 03 '23 13:01

Devesh Kumar Singh