← Back to list

One of the Most important file in Python project — Makefile

A good programmer is a lazy programmer! As a developer, we always want to automize something, reduce the time of typing the long bash…

Wei hung Pan · 2022-09-30 14:09 · 334 claps · 3.5 min read
#makefile #python #software-development #software-engineering #ci-cd-pipeline
Open on Medium ↗
Wiki topics: ☁️ · DevOps & Cloud

One of the Most important file in Python project — Makefile

Makefile in python package

Makefile in python package

A good programmer is a lazy programmer! As a developer, we always want to automize something, reduce the time of typing the long bash command, avoid spelling error…etc. Especially in some repeated tasks while developing, such as

  • Install the package and setting the environment
  • Check the code rule and style
  • Run tests with coverage (Continuous Integration)
  • Continuous deployment with Docker

In this case **Makefile (or `makefile`**) is your good friend!

Dockerfile is also important for the package development, see ***my another post.***

What is a Makefile?

Makefile is a standard file format to automate software building procedures and other complex tasks in an easy way. Actually, it just contains a list of shell commands.

It was originally created by Stuart Feldman in April 1976. But it is still very useful nowadays in software development.

Makefile structure

A makefile is usually just named exactly that, makefile or Makefile, and generally has the following structure:

[make code]
recipe_name:
    [shell code]
another_recipe_name:
    [shell code]

The example is like this

[embed]

In this case, make code and contains several recipes, hello, install, test.. If we thus call make hello, we get the following output:

$ make hello
echo "Hello world!"
Hello world!

By default, if we just run make, it runs by the first recipe as below

$ make
echo "Hello world!"
Hello world!

@’s / .SILENT

If we only want to print the output, we can prefix each shell script line with a “@”, resulting in the following makefile:

The result will be

$ make hello
Hello world!

It is the same if you add .SILENT: in front of the recipe, as follow, we got the same result

$ make hello
Hello world!

Environment Variable

You can also add the environmental variable in the makefile , like the example below.

In this case when you run the code, you will get this

$ make hello
Hello world!
My name is Pan

Using .PHONY

[embed]The meaning of the '.PHONY' target inside a Makefile Inside the Makefile of a project you can find a .PHONY element followed by a label and then the same label used as a…dev.to

Sometime we can find a .PHONY element followed by a label and then the same label used as a recipe for the Makefile like below

What does that mean? What is the difference with only the recipe without .PHONY?

In the general case, if there is no file called install in the project, there is no difference. However, if there was a file named install exists in the same directory as the Makefile, then make install will do nothing.

Long story short in this example:

.PHONY: install
  • means the word “install” doesn’t represent a file name in this Makefile;
  • means the Makefile has nothing to do with a file called “install” in the same directory.

[embed]What is the purpose of .PHONY in a Makefile? By default, Makefile targets are "file targets" - they are used to build files from other files. Make assumes its…stackoverflow.com

Recipes I use frequent

The recipes that I use regularly in my work are the following:

  • install, you can set up a requirement.txt for giving all your dependence in the file, and use make install . It can also help in setting up the python version as well as the docker version.
  • test, which runs all tests, including the lint check, unit test, integration test within the project .
  • format, which can run black and isort automatically to adjust the coding style.

TL;DR

Using makefile in python package developmentis essential and necessary.

By using different recipes in the makefile, you can easily set up the environment of new projects, manage tests and even deploy to AWS. It also makes the development process much more painless for collaborators to maintain and co-working on the current code structure.

If you like my article, you can become member of Medium through my Medium page , thanks

Sources

[embed]Make-fu - Setting up a makefile for a Python project There's a common saying among computer scientists that "A good programmer is a lazy programmer". Coincidentally…saattrupdan.github.io

Very detail and impressive one! My post is written along with line of this post from Dan

[embed]

This is the Github which is mentioned in the above viedo https://github.com/noahgift/fastapi


메타데이터
post_id
722e86e1c8ea
slug
one-of-the-most-important-file-in-python-project-makefile-722e86e1c8ea
url
https://medium.com/@n124080/one-of-the-most-important-file-in-python-project-makefile-722e86e1c8ea
canonical_url
https://medium.com/@n124080/one-of-the-most-important-file-in-python-project-makefile-722e86e1c8ea
author_url
https://medium.com/@n124080
status
ok
fetched_at
2026-06-15 20:49:13