I like a comfortable life. I like to open some repository and as naturally as possible to run the example and see how the project works. Ideally, just calling one script, and that’s it.
Python is fantastic, 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 do 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 straightforward and good to use:
.PHONY: help prepare-dev test lint run doc
VENV_NAME?=venv
VENV_ACTIVATE=. $(VENV_NAME)/bin/activate
PYTHON=${VENV_NAME}/bin/python3
.DEFAULT: help
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"
prepare-dev:
sudo apt-get -y install python3.5 python3-pip
python3 -m pip install virtualenv
make venv
# Requirements are in setup.py, so whenever setup.py is changed, re-run installation of dependencies.
venv: $(VENV_NAME)/bin/activate
$(VENV_NAME)/bin/activate: setup.py
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
${PYTHON} app.py
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 setup.py
. Makefile will run venv target only when you remove (or don’t have yet) virtual environment or you change setup.py
. 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 regular 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 that 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: https://github.com/horejsek/python-webapp-example