Makefile with Python

en in code • 4 min read

I like easy life. I like to open some repository and as simply as possible to run the example and see how the project works. Ideally, just calling one script and that’s it.

Python is awesome, Python makes a lot of things very easy. But tooling around, mostly packaging, is not working well. It has a lot of flaws. I’m used to fix this with Makefile. Some people are furious about that and argue that it’s only for building C codes. Well… maybe. But I prefer to be practical.

Check out why I think it’s very simple and good to use:

.PHONY: help prepare-dev test lint run doc

VENV_ACTIVATE=. $(VENV_NAME)/bin/activate

.DEFAULT: help
    @echo "make prepare-dev"
    @echo "       prepare development environment, use only once"
    @echo "make test"
    @echo "       run tests"
    @echo "make lint"
    @echo "       run pylint and mypy"
    @echo "make run"
    @echo "       run project"
    @echo "make doc"
    @echo "       build sphinx documentation"

    sudo apt-get -y install python3.5 python3-pip
    python3 -m pip install virtualenv
    make venv

# Requirements are in, so whenever is changed, re-run installation of dependencies.
venv: $(VENV_NAME)/bin/activate
    test -d $(VENV_NAME) || virtualenv -p python3 $(VENV_NAME)
    ${PYTHON} -m pip install -U pip
    ${PYTHON} -m pip install -e .
    touch $(VENV_NAME)/bin/activate

test: venv
    ${PYTHON} -m pytest

lint: venv
    ${PYTHON} -m pylint
    ${PYTHON} -m mypy

run: venv

doc: venv
    $(VENV_ACTIVATE) && cd docs; make html

It’s a very simple Makefile which I like to start with any project. It helps me to install all dependencies I need without searching and run the project (or tests, or lints or whatever) without remembering how the tool for it look like.

The best feature is how Makefile works. Notice venv target. It says that it needs to have script venv/bin/activate which needs Makefile will run venv target only when you remove (or don’t have yet) virtual environment or you change Because I use venv as a dependency for all tasks, I can change my Python dependencies and just run the tests. Makefile will ensure that a virtual environment is updated.

So… maybe Makefile is only for compiling codes… but anyway I think this is very clever. I could use normal bash scripts, sure, but why? It’s hard to keep them executable in git repository, not fast to type and I would need to write logic which Makefile already provides.

Not convinced? Well, I don’t force you to use it, I’m just saying why I love to use them. :-)

Whole example usage you can find here:

Share on:   Facebook   Twitter   Reddit   Tumblr   Pinterest

You may also like

en Where is Zen of Python?, October 29, 2019
en What Makes Good Program?, November 20, 2018
en Old Code, October 31, 2018
en Fast JSON Schema for Python, October 1, 2018
en Open Source Responsibilities, September 6, 2018

Popular from code

en Deployment of Python Apps, August 15, 2018
cs Jasně, umím Git…, August 6, 2014
cs Checklist na zabezpečení webových aplikací, March 1, 2016
cs Pokročilé regulární výrazy, August 17, 2014
cs Co se mi líbí na pytestu, February 16, 2015