Using a Makefile with a Python Project

Using a Makefile 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.

  1. Create a project folder:

    mkdir my_python_project
    cd my_python_project
    
  2. 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
    
  3. Add code to main.py:
    Open scripts/main.py and add this simple code:

    def greet(name):
        return f"Hello, {name}!"
    
    if __name__ == "__main__":
        print(greet("World"))
    
  4. Add code to test_main.py:
    Open tests/test_main.py and add this test code:

    from scripts.main import greet
    
    def test_greet():
        assert greet("Alice") == "Hello, Alice!"
        assert greet("Bob") == "Hello, Bob!"
    
  5. Install pytest:
    We’ll use pytest to 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.

  1. Create the Makefile:
    In the root of your project folder (my_python_project), create a file named Makefile:

    touch Makefile
    
  2. Add content to the Makefile:
    Open the Makefile and 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.

  1. Run the project:
    To run the main script, use:

    make run
    

    Output:

    Running the project...
    Hello, World!
    
  2. Run tests:
    To run the tests, use:

    make test
    

    Output:

    Running tests...
    ============================= test session starts ==============================
    collected 1 item
    
    tests/test_main.py .                                                     [100%]
    
    =============================== 1 passed in 0.01s ===============================
    
  3. Clean up files:
    To clean up temporary files like __pycache__, use:

    make clean
    

    Output:

    Cleaning up...
    
  4. Show help:
    To see a list of available tasks, use:

    make help
    

    Output:

    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:


Why Use a Makefile?

  1. Save Time: You don’t have to type long commands every time.

  2. Stay Consistent: The same commands are run the same way every time.

  3. Document Your Workflow: A Makefile is like a cheat sheet for your project tasks.

  4. 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!

ദ്ദി ˉ͈̀꒳ˉ͈́ )✧ M.T.

© 2025

Instagram 𝕏 GitHub Linkedin