Created
April 3, 2019 10:12
-
-
Save eeichinger/739c9e19d6b8aaabbfaf8b8373f6b41b to your computer and use it in GitHub Desktop.
Makefile 'init' target for setting up a virtual python environment (using pyenv)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# automatically initialise a local python env | |
# requires: | |
# - pyenv installed (see https://github.com/pyenv/pyenv) | |
# - ./requirements.txt to manage dependencies via pip | |
PYENV_VERSION:="3.7.2" | |
all: init | |
.PHONY: clean | |
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 | |
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'; \ | |
fi; | |
@if [ ! -f ./.venv/bin/python ]; then \ | |
echo "Create virtual environment .venv"; \ | |
eval "`pyenv which python` -m venv .venv"; \ | |
fi | |
@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