Each time I start new pyton project my first step is to create a git repository, virtual environment and requirements.txt file. All project dependencies listed in the requirements.txt file are installed in the local venv. As I am a big fan of make utility, I usually create a Makefile with few targets that help to manage the project.

I need to be able to:

  • activate virtualenv
  • install requirements listed in the requirments.txt file
  • install the project
  • run tests
  • clean files and directories
  • build the wheel and tar distributions

Assumptions:

  • python virtualenv is installed

Below is the small code snippet that ilustrates how to combine virtualenv and make.

# Makefile
 
venv: venv/bin/activate
venv/bin/activate: requirements.txt
        test -d venv || virtualenv venv
        venv/bin/pip install -Ur requirements.txt
        touch venv/bin/activate

devbuild: venv
        venv/bin/python setup.py install

devinstall: venv
        venv/bin/pip install --editable .

test: venv
        venv/bin/pytest tests/test_vm.py -v

How does it work? The venv target builds virtualenvironment and installs Python packages listed in the requirements.txt file. If all packages are installed and up to date nothing happens. The venv make target can be used as a dependency for other targets or called directly to build venv from scratch:

$ make venv