Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Makefile for python scripts

I have files to send but I am a little bit confused on how to do it. I had to write two codes in python (say A.py and B.py) and it is required to create a Makefile such that typing "make" in bash in the folder would create the executables for A and B. I do not have Unix and I never used it. I'm a Windows user. Is this something I should write in my .py files? And how can I check if I am doing it right if I'm on Windows? I saw this post Python Script Executed with Makefile but I am not sure to understand how this works. Thank you for your help

like image 637
CTXR Avatar asked Aug 31 '25 17:08

CTXR


2 Answers

Let's take the example you shared in a link.

foo: do-foo.py
    python do-foo.py

Makefile is like a cookbook. To create a dish foo you need an ingredient do-foo.py. If do-foo.py is present then the Makefile can execute python script python do-foo.py, which will cook a dish foo (execute it). If not, the script will not be executed and a foo dish not cooked.

With this similar analogy in your example you would do:

all: exe_A exe_B

exe_A: A.py 
    python A.py

exe_B: B.py 
    python B.py

And how can I check if I am doing it right if I'm on Windows?

To check it, I would recommend that you install a UNIX simulator on windows called Cygwin. And then run it from there as make all. There are tutorials on it.

EDIT: Corrected my mistake pointed out by Jorengarenar

like image 167
Drejc Avatar answered Sep 02 '25 08:09

Drejc


Python is interpreted language - it doesn't produce executables like C or C++ would do. The source code itself "is executable". To execute program A.py you need to run command python A.py.

On *nix systems, you can omit the need of writing python every time and make file itself executable by adding so called shebang (#!/usr/bin/env python) at the top of script and adding executable flag with chmod +x A.py (only once). Now script could be executed just with ./A.py (or A.py if the directory containing it is in PATH environment variable).

Makefile can execute commands, just as the shell script can do (obliviously there are differences, but it's not important for now). So if you want to execute your scripts with make, you can just do:

foo: A.py B.py
    python A.py
    python B.py
like image 38
Jorengarenar Avatar answered Sep 02 '25 07:09

Jorengarenar