Wednesday, April 29, 2026

Makefiles case in Python Projects (and how to start)

Share

Makefiles case in Python Projects (and how to start)Photo by the author Ideogram

# Entry

Imagine it: you are working on the Python project and every time you want to start the tests, you enter python3 -m pytest tests/ --verbose --cov=src. When you want to format your code, it is black . && isort .. For flutting, you run flake8 src tests. Before you know, you juggle a dozen different commands, and your teammates do the same slightly differently.

Makefile is useful here. Originally used for C and C ++ projects, Makefiles can be very useful in Python’s development as a elementary way of standardization and automation of joint tasks. Think about makeup as one place where you define shortcuts for all things you do many times.

# Why employ makefiles in Python projects?

Cohesion throughout the team
When everyone in your team is running make test Instead of remembering the exact command of Pytes with all his flags, you eliminate the problem of “work on my machine”. Fresh team members can jump and immediately know how to start tests, format the code or implement the application.

Documentation that actually works
Unlike Readme files that are antiquated, Makefiles serve as useful documentation. When someone runs make assistThey see exactly what tasks are available and how to employ them.

Simplified sophisticated work flows
Some tasks require many steps. Perhaps you need to install dependencies, start migrations, test data, and then start the programming server. It becomes a single with makefile make dev order.

# Start of the first Python Makefile

Let’s build practical Makefile step by step. Create a file called Makefile (without extension) in root project.

// Basic structure and aid command

This code creates an automatic Makefile assist system, which displays all available commands with their descriptions:

.PHONY: assist
assist:  ## Show this assist message
	@echo "Available commands:"
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "  33[36m%-15s33[0m %sn", $$1, $$2}'

.DEFAULT_GOAL := assist

. .PHONY: assist He says that “help” is not a real file, but a command to run. When you enter make assistFirst, he prints “available commands:” then uses a combination grep AND awk To scan Makefile himself, find all the poems that have the names of commands and then ## descriptionAnd format them in a nice readable list with command names and their explanations.

// Environmental configuration

This code creates three environmental management commands:

.PHONY: install
install:  ## Install dependencies
	pip install -r requirements.txt
	pip install -r requirements-dev.txt

.PHONY: venv
venv:  ## Create virtual environment
	python3 -m venv venv
	@echo "Activate with: source venv/bin/activate"

.PHONY: tidy
tidy:  ## Immaculate up cache files and build artifacts
	find . -type f -name "*.pyc" -delete
	find . -type d -name "__pycache__" -delete
	find . -type d -name "*.egg-info" -exec rm -rf {} +
	rm -rf build/ dist/ .coverage htmlcov/ .pytest_cache/

. install The command launches PIP twice to install both main dependencies and programming tools from requirements. The VENV command creates a fresh Python virtual folder called “Venv” and prints instructions on its activation.

. tidy The command removes all messy files that Python creates during development. Removes compiled Python files (.Pyc), cache folders (Pyc), Packaging of information catalogs and build artifacts such as coverage and cache reports.

// Code quality and testing

It creates code quality commands:

.PHONY: format
format:  ## Format code with black and isort
	black .
	isort .

.PHONY: lint
lint:  ## Run linting checks
	flake8 src tests
	black --check .
	isort --check-only .

.PHONY: test
test:  ## Run tests
	python -m pytest tests/ --verbose

.PHONY: test-cov
test-cov:  ## Run tests with coverage
	python -m pytest tests/ --verbose --cov=src --cov-report=html --cov-report=term

.PHONY: check
check: lint test  ## Run all checks (lint + test)

. format The command automatically repairs the code style using black for formatting and ISORT for import organizations.

The Lint command checks that the code is in line with the rules of the style without changing anything. Flake8 finds violations of style, while Black and Izort operated only in check -in mode to see if formatting is needed.

. test The command launches the test package. test-cov It launches tests, and also measures the code covering and generates reports. . check The command triggers both broadcasting and testing together, depending on lint AND test commands.

// Development of developmental work

This creates the orders of the development program:

.PHONY: dev
dev: install  ## Set up development environment
	@echo "Development environment ready!"
	@echo "Run 'make serve' to start the development server"

.PHONY: serve
serve:  ## Start development server
	python3 -m flask run --debug

.PHONY: shell
shell:  ## Start Python shell with app context
	python3 -c "from src.app import create_app; app=create_app(); app.app_context().push(); import IPython; IPython.start_ipython()"

. dev The command first starts install The command to configure the dependence, and then prints success messages with the next steps. . serve The command launches the flask development server in debug mode.

. shell The command launches the iPithon coating, which is already connected to the context of the Flask application, so you can interactally test the database queries and application functions without manually importing everything.

# More Makefile techniques

// Using variables

You can define variables to avoid repetition:

PYTHON := python3
TEST_PATH := tests/
SRC_PATH := src/

.PHONY: test
test:  ## Run tests
	$(PYTHON) -m pytest $(TEST_PATH) --verbose

// Conditional commands

Sometimes you want different behaviors based on the environment:

.PHONY: deploy
deploy:  ## Deploy application
ifeq ($(ENV),production)
	@echo "Deploying to production..."
	# Production deployment commands
else
	@echo "Deploying to staging..."
	# Staging deployment commands
endif

// File dependencies

You can create files dependent goals, so they only work if necessary:

requirements.txt: pyproject.toml
	pip-compile pyproject.toml

.PHONY: sync-deps
sync-deps: requirements.txt  ## Sync dependencies
	pip-sync requirements.txt

🔗 This is an example Complete Makefile for the Flask internet application.

# Best practices and tips

Here are some of the best imitation practices when writing makefiles:

  • Do not complicate your makefile too much. If the task is sophisticated, consider transferring logic to a separate script and calling it with make.
  • Choose command names that clearly indicate what they are doing. Make test is better than making T, and Deved-Setup is clearer than configuration.
  • In the case of commands that do not create files, always declare them as. This prevents problems if someone creates a file of the same name as your command.
  • Organize your makefile to group the functionality related to the group together.
  • Make sure all your commands work from a fresh maple repository. Nothing frustrates fresh colleagues, such as a broken configuration process.

# Application

Makefiles may seem like an ancient school tool, but they are effective in Python’s projects. They provide a coherent interface for typical tasks and assist fresh colleagues quickly get productivity.

Create basic Makefile using installation, testing and assistance commands. As the project increases, and the work flow becomes more sophisticated, you can add more goals and dependencies if necessary.

Remember that the goal is not to create the most clever or sophisticated makeup. This is to facilitate everyday development and more reliable tasks. Probably, keep useful and let your Makefile become a command center, which brings order to your chaos Python Project.

Bala Priya C He is a programmer and technical writer from India. He likes to work at the intersection of mathematics, programming, data science and content creation. Its interest areas and specialist knowledge include Devops, Data Science and Natural Language Processing. He likes to read, write, cod and coffee! He is currently working on learning and sharing his knowledge of programmers, creating tutorials, guides, opinions and many others. Bal also creates a coding resource and tutorial review.

Latest Posts

More News