Python – Invoke python script through make command

makepython

I have a tool with 5 python scripts say A.py, B.py, C.py, D.py and E.py. I have to run this program in Linux. A.py is the one program I run which imports other files and uses them. For running A.py, I use the command

$ python A.py

Until now the input and output paths are hardcoded in A.py. Now I am supposed to write a makefile with all the file structure and dependencies. This makefile should also include the command for executing python scrip A.py. The required input and output paths has to be understood when make makefile command is invoked and the tool should start running. How to do this?

EDITED :

The makefile should be something like this holding all these details.

INPUT_PATH = list of all input paths
FILES = list of .c files located in the above specified paths
OUTPUT_PATH = output path where generated file has to be stored
command to execute python scrip:
   A.py inputpath+filename outputpath

so whenever there a new input .c files, they will be added to the list in makefile. Any sample makefile of this kind that you could suggest?

Best Answer

In python you get the commandline parameters from sys.argv so instead of

#!/usr/bin/env python
base_dir = /hard/coded/path

you do:

#!/usr/bin/env python
import sys
base_dir = sys.argv[1]

(putting in checks might be appropriate). If you explicitly start the program with python, you can leave out the first line, but it won't hurt to be there in case you make A.py executable, you will need it.

In your makefile you now can specify:

default:
        python A.py /hard/coded/path

(make sure you have a Tabbefore python, not 8 spaces.

And then you can run make (no need to do make makefile unless you have a Makefile or with Gnu make a GNUmakefile in the same directory)

Related Question