Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a python function using make

I have a Python script named sample.py containing multiple functions.

def A():
    return 3

def B():
    return [i for i in range(2,10)]

I have to write a Makefile which calls the function B from sample.py and stores its output to another file (say output.txt); i.e. I wish to run make output.txt from the command line and store the result of calling function B from sample.py. How do I write the recipe for it? We can call a script directly from the Makefile, but how do I call a particular function?

output.txt:sample.py
    ___recipe___
like image 547
Aditya Mishra Avatar asked Apr 06 '26 21:04

Aditya Mishra


1 Answers

python -c <cmd> allows you to pass a string of Python code to the interpreter. So we just need to write a one-liner script (semicolons do work in Python; they just normally aren't necessary and are discouraged) that imports the .py file and calls the function. Something like:

output.txt: sample.py
    python -c "import sample; print(sample.B())" > output.txt
like image 99
Karl Knechtel Avatar answered Apr 08 '26 15:04

Karl Knechtel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!