Skip to content

Instantly share code, notes, and snippets.

Created Apr 3, 2019
What would you like to do?
Makefile 'init' target for setting up a virtual python environment (using pyenv)
# automatically initialise a local python env
# requires:
# - pyenv installed (see
# - ./requirements.txt to manage dependencies via pip
all: init
.PHONY: clean
@echo "Cleaning files"
@find . -name "*.pyc" -type f -exec rm -f '{}' \;
@find . -name "__pycache__" -type d -prune -exec rm -rf '{}' \;
@find . -name ".pytest_cache" -type d -prune -exec rm -rf {} \;
@rm -rf ./.tox
@rm -f ./.coverage
.PHONY: init
@echo "Initialising local python dev environment"
@echo "Checking required python version manager (pyenv)"
@if ! type 'pyenv' > /dev/null 2>&1; then echo 'Error: pyenv missing (on OSX run "brew install pyenv")'; exit 1; fi
@echo "${PYENV_VERSION}" > .python-version; fi
@echo "Ensure required python version `pyenv local` (specified in file ./.python-version) is installed:"
@pyenv install -s
@if [ -f ./.venv/bin/python ] && [ "$(./.venv/bin/python --version)" != "Python `pyenv local`" ]; then \
echo '.venv version '`./.venv/bin/python --version`' does not match version '`pyenv local`' required in .python-version: reinstalling .venv...'; \
rm -rf .venv; \
else \
echo 'OK'; \
@if [ ! -f ./.venv/bin/python ]; then \
echo "Create virtual environment .venv"; \
eval "`pyenv which python` -m venv .venv"; \
@echo "Virtual environment installed into ./.venv with python version `pyenv local`"
@echo "Installing requirements"; \
./.venv/bin/python -m pip install --upgrade pip 1>/dev/null; \
./.venv/bin/python -m pip install -r requirements.txt 1>/dev/null; \
if [ -f test-requirements.txt ]; then ./.venv/bin/python -m pip install -r test-requirements.txt 1>/dev/null; fi
@echo "init complete"
@echo "IMPORTANT: Dont forget to run 'source ./.venv/bin/activate' to activate the local python environment!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment