โ๏ธ Python Makefile Guide
Beginner-friendly documentation about Makefiles for Python projects, automation workflows, linting, virtual environments, and common development commands.
๐ A complete example Makefile is provided alongside this guide for reference and practical implementation examples.
๐ Table of Contents
- ๐ What is a Makefile?
- ๐ Why Use Makefiles in Python?
- ๐ ๏ธ Common Python Makefile Commands
- ๐งฑ Basic Makefile Structure
- ๐ Conditional Statements in Makefiles
- ๐ฏ Targets
- ๐ฆ Variables
- ๐ Default Targets
- ๐ Dependencies
- ๐งช Virtual Environment Example
- ๐ฅ Installing Requirements
- ๐ Linting Example
- ๐งน Cleaning Cache Files
- ๐งจ Full Clean Example
- โป๏ธ Rebuild Example
- ๐ .PHONY
- โ ๏ธ Common Beginner Mistakes
- ๐ง Recommended Workflow
- ๐ Additional Notes
- ๐ Final Notes
๐ What is a Makefile?
A Makefile is a file used to automate terminal commands.
Instead of typing long commands repeatedly, you can run:
make
or:
make run
and execute predefined workflows automatically.
๐ Why Use Makefiles in Python?
Makefiles help: - automate repetitive tasks - standardize project workflows - simplify setup - improve teamwork - reduce mistakes
Very useful for: - 42 projects - game projects - parsers - APIs - collaborative repositories
๐ ๏ธ Common Python Makefile Commands
| โ๏ธ Target | ๐ Purpose |
|---|---|
make |
๐ Default workflow |
make run |
โถ๏ธ Run project |
make install |
๐ฆ Install dependencies |
make lint |
๐ Run flake8/mypy |
make clean |
๐งน Remove cache |
make re |
โป๏ธ Rebuild project |
๐งฑ Basic Makefile Structure
Simple Example
run:
python3 main.py
Execute with the command:
make run
๐ Conditional Statements in Makefiles
Makefiles can also use shell conditionals such as:
@if [ ! -d "$(VENV)" ]; then \
python3 -m venv $(VENV); \
fi
What This Does
This checks if the virtual environment folder already exists.
Explanation
| Part | Meaning |
|---|---|
if |
Starts the condition |
[ ! -d "$(VENV)" ] |
Checks if the directory does NOT exist |
then |
Executes the commands below |
fi |
Ends the condition |
Why This is Useful
This prevents: - recreating the virtual environment every time - reinstalling dependencies unnecessarily - accidentally overwriting environments
Very common in Python project setup workflows.
Common Example
venv:
@if [ ! -d "$(VENV)" ]; then \
python3 -m venv $(VENV); \
fi
This means:
- if venv/ does not exist โ create it
- otherwise โ do nothing
๐ฏ Targets
A target is a command section inside the Makefile.
Example
install:
pip install -r requirements.txt
๐ฆ Variables
To Makefiles cleaner and reusable.
Variables are useful to: - reduce repetition - simplify updates - improve readability
Example
PYTHON = python3
Used like:
$(PYTHON) main.py
๐ Default Targets
The first target is usually the default target.
Example
all: run
Running:
make
automatically runs:
make run
๐ Dependencies
Targets can depend on other targets.
Example
run: install
python3 main.py
Meaning:
- install runs first
- then run
๐งช Virtual Environment Example
venv:
python3 -m venv venv
Using the venv Python
run:
venv/bin/python3 main.py
This guarantees: - correct packages - correct environment
๐ฅ Installing Requirements
install:
venv/bin/pip install -r requirements.txt
๐ Linting Example
lint:
flake8 .
mypy .
Useful for: - formatting checks - typing checks
๐งน Cleaning Cache Files
clean:
find . -name "__pycache__" -exec rm -rf {} +
๐งจ Full Clean Example
fclean: clean
rm -rf venv
Removes: - cache files - virtual environment
โป๏ธ Rebuild Example
re: fclean install
clean everything
โ reinstall everything
๐ .PHONY
.PHONY prevents Make from confusing targets with real files.
Example
.PHONY: run clean install
Very common in professional Makefiles.
โ ๏ธ Common Beginner Mistakes
โ Using Spaces Instead of Tabs
Wrong:
run:
python3 main.py
Correct:
run:
python3 main.py
โ Missing .PHONY
Without .PHONY, Make may behave incorrectly if files share target names.
โ Using Global Python
Bad:
python3 main.py
Better:
venv/bin/python3 main.py
๐ง Recommended Workflow
- Create virtual environment
- Install requirements
- Run project
- Run linting
- Clean cache files when needed
๐ Additional Notes
An example Makefile is included alongside this document for reference and practical usage examples.
๐ Final Notes
Understanding Makefiles is an important skill for Python development.
Even simple Makefiles can: - save time - automate repetitive tasks - standardize workflows - improve project organization