Last active
January 2, 2025 14:55
-
-
Save genyrosk/50196ad03231093b604cdd205f7e5e0d to your computer and use it in GitHub Desktop.
Makefile for a Python environment with virtualenv
This file contains hidden or 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
SHELL:=/bin/bash | |
VIRTUAL_ENV=env | |
JUPYTER_ENV_NAME=env | |
PORT=8888 | |
PYTHON=${VIRTUAL_ENV}/bin/python3 | |
# .ONESHELL: | |
DEFAULT_GOAL: help | |
.PHONY: help run clean build venv ipykernel update jupyter | |
# Colors for echos | |
ccend = $(shell tput sgr0) | |
ccbold = $(shell tput bold) | |
ccgreen = $(shell tput setaf 2) | |
ccso = $(shell tput smso) | |
clean: ## >> remove all environment and build files | |
@echo "" | |
@echo "$(ccso)--> Removing virtual environment $(ccend)" | |
rm -rf $(VIRTUAL_ENV) | |
build: ##@main >> build the virtual environment with an ipykernel for jupyter and install requirements | |
@echo "" | |
@echo "$(ccso)--> Build $(ccend)" | |
$(MAKE) clean | |
$(MAKE) install | |
$(MAKE) ipykernel | |
venv: $(VIRTUAL_ENV) ## >> install virtualenv and setup the virtual environment | |
$(VIRTUAL_ENV): | |
@echo "$(ccso)--> Install and setup virtualenv $(ccend)" | |
python3 -m pip install --upgrade pip | |
python3 -m pip install virtualenv | |
virtualenv $(VIRTUAL_ENV) | |
install: venv requirements.txt ##@main >> update requirements.txt inside the virtual environment | |
@echo "$(ccso)--> Updating packages $(ccend)" | |
$(PYTHON) -m pip install -r requirements.txt | |
ipykernel: venv ##@main >> install a Jupyter iPython kernel using our virtual environment | |
@echo "" | |
@echo "$(ccso)--> Install ipykernel to be used by jupyter notebooks $(ccend)" | |
$(PYTHON) -m pip install ipykernel jupyter jupyter_contrib_nbextensions | |
$(PYTHON) -m ipykernel install | |
--user | |
--name=$(VIRTUAL_ENV) | |
--display-name=$(JUPYTER_ENV_NAME) | |
$(PYTHON) -m jupyter nbextension enable --py widgetsnbextension --sys-prefix | |
jupyter: venv ##@main >> start a jupyter notebook | |
@echo "" | |
@"$(ccso)--> Running jupyter notebook on port $(PORT) $(ccend)" | |
jupyter notebook --port $(PORT) | |
# And add help text after each target name starting with '\#\#' | |
# A category can be added with @category | |
HELP_FUN = \ | |
%help; \ | |
while(<>) { push @{$$help{$$2 // 'options'}}, [$$1, $$3] if /^([a-zA-Z\-\$\(]+)\s*:.*\#\#(?:@([a-zA-Z\-\)]+))?\s(.*)$$/ }; \ | |
print "usage: make [target]\n\n"; \ | |
for (sort keys %help) { \ | |
print "${WHITE}$$_:${RESET}\n"; \ | |
for (@{$$help{$$_}}) { \ | |
$$sep = " " x (32 - length $$_->[0]); \ | |
print " ${YELLOW}$$_->[0]${RESET}$$sep${GREEN}$$_->[1]${RESET}\n"; \ | |
}; \ | |
print "\n"; } | |
help: ##@other >> Show this help. | |
@perl -e '$(HELP_FUN)' $(MAKEFILE_LIST) | |
@echo "" | |
@echo "Note: to activate the environment in your local shell type:" | |
@echo " $$ source $(VIRTUAL_ENV)/bin/activate" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment