In this tutorial, we’ll create a small Python project and use a Makefile to automate common tasks like running the project, testing, and cleaning up files. By the end, you’ll have a clear understanding of how to use a Makefile to make your workflow easier.
Step 1: Set Up the Python Project
Let’s start by creating a simple Python project.
-
Create a project folder:
mkdir my_python_project cd my_python_project -
Create the project files:
-
scripts/main.py: The main script for the project. -
tests/test_main.py: A test file for the main script.
Run these commands to create the files:
mkdir scripts tests touch scripts/main.py tests/test_main.py -
-
Add code to
main.py:
Openscripts/main.pyand add this simple code:def greet(name): return f"Hello, {name}!" if __name__ == "__main__": print(greet("World")) -
Add code to
test_main.py:
Opentests/test_main.pyand add this test code:from scripts.main import greet def test_greet(): assert greet("Alice") == "Hello, Alice!" assert greet("Bob") == "Hello, Bob!" -
Install
pytest:
We’ll usepytestto run tests. Install it with:pip install pytest
Step 2: Create the Makefile
Now, let’s create a Makefile to automate tasks like running the project, testing, and cleaning up.
-
Create the Makefile:
In the root of your project folder (my_python_project), create a file namedMakefile:touch Makefile -
Add content to the Makefile:
Open theMakefileand add the following:# Variables PYTHON = python3 TEST_DIR = tests SCRIPTS_DIR = scripts # Default task (runs when you type `make`) all: run # Run the main script run: @echo "Running the project..." $(PYTHON) $(SCRIPTS_DIR)/main.py # Run tests test: @echo "Running tests..." $(PYTHON) -m pytest $(TEST_DIR) # Clean up temporary files clean: @echo "Cleaning up..." rm -rf __pycache__ rm -rf $(TEST_DIR)/__pycache__ rm -rf $(SCRIPTS_DIR)/__pycache__ # Show help help: @echo "Available tasks:" @echo " run - Run the project" @echo " test - Run tests" @echo " clean - Clean up files" @echo " help - Show this help message"
Step 3: Use the Makefile
Now that the Makefile is ready, let’s use it to run tasks.
-
Run the project:
To run the main script, use:make runOutput:
Running the project... Hello, World! -
Run tests:
To run the tests, use:make testOutput:
Running tests... ============================= test session starts ============================== collected 1 item tests/test_main.py . [100%] =============================== 1 passed in 0.01s =============================== -
Clean up files:
To clean up temporary files like__pycache__, use:make cleanOutput:
Cleaning up... -
Show help:
To see a list of available tasks, use:make helpOutput:
Available tasks: run - Run the project test - Run tests clean - Clean up files help - Show this help message
Step 4: Customize the Makefile
You can add more tasks to the Makefile as your project grows. For example:
-
Install dependencies:
Add a task to install Python dependencies:install: @echo "Installing dependencies..." pip install -r requirements.txt -
Format code:
Add a task to format code usingblack:format: @echo "Formatting code..." black $(SCRIPTS_DIR) $(TEST_DIR)
Why Use a Makefile?
-
Save Time: You don’t have to type long commands every time.
-
Stay Consistent: The same commands are run the same way every time.
-
Document Your Workflow: A Makefile is like a cheat sheet for your project tasks.
-
Share with Others: If someone else works on your project, they can use the same Makefile.
Using a Makefile with a Python project is a simple way to automate tasks and make your workflow smoother. Whether you’re running scripts, testing, or cleaning up files, a Makefile can save you time and effort.
Try creating a Makefile for your next project and see how much easier it makes your work!