-
Notifications
You must be signed in to change notification settings - Fork 1
Project Makefile using Pip Tools
Michael Goerz edited this page Dec 13, 2023
·
2 revisions
.PHONY: help init jupyter-notebook jupyter-lab clean clean-venv distclean
PYTHON = .venv/bin/python
help: ## Show this help
@grep -E '^([a-zA-Z_-]+):.*## ' $(MAKEFILE_LIST) | awk -F ':.*## ' '{printf "%-20s %s\n", $$1, $$2}'
$(PYTHON): requirements.in
python3 -m venv .venv
$@ -m pip install --upgrade pip
$@ -m pip install pip-tools
@touch $@ # mark updated
requirements.txt: $(PYTHON) requirements.in
$(PYTHON) -m piptools compile -o $@ requirements.in
$(PYTHON) -m piptools sync
@touch $@ # mark updated
init: requirements.txt ## Create the virtual project environment
jupyter-notebook: requirements.txt ## Run a Jupyter notebook server
jupyter notebook
jupyter-lab: requirements.txt ## Run a Jupyter lab server
jupyter lab
clean: ## Remove generated files
clean-venv: ## Remove environment
rm -rf .venv
distclean: clean clean-venv ## Restore clean repository state
rm -rf *.ipynb # if using jupytext
rm -f requirements.txt
rm -rf .ipynb_checkpointsNotes:
- The
helptarget could also be implemented with a Python script - The version of Python used to create the
.venvfolder is the version of Python to be used for the project. Consider using, e.g.,python3.10instead ofpython3. - Since
pip-toolis installed automatically into.venv, it is not a dependency to bootstrap the project. - Instead of
requirements.in, requirements could also be specified inpyproject.toml, see thepip-toolsdocumenrtation - The
requirements.infile must includeipykernel(the "project kernel") - The
jupyterexecutable is not (and should not be) part of the.venv. Instead, there should be a single "site" installation of Jupyter. - To make the project kernel available to the site Jupyter installation, make sure that
python-localvenv-kernelis installed into the same environment as Jupyter. All notebooks should use this kernel. - The
cleantarget should be used to remove, e.g.,__pycache__files, if applicable. - The
distcleantarget should remove*.ipynbfiles only if the Jupytext extension is used. Note that this removes the output of all notebooks. If your notebooks take a long time to run, take steps as to not accidentally delete the notebooks. - When the project is "stable" / "finished", consider adding
requirements.txtto source control (and then do not remove it inmake distclean)
If it is not possible to install python-localvenv-kernel, the project kernel must be installed into the user's Jupyter data directory. To this end, we can add install and uninstall targets:
.PHONY: help init install uninstall jupyter-notebook jupyter-lab clean clean-venv distclean
PYTHON = .venv/bin/python
help: ## Show this help
@grep -E '^([a-zA-Z_-]+):.*## ' $(MAKEFILE_LIST) | awk -F ':.*## ' '{printf "%-20s %s\n", $$1, $$2}'
$(PYTHON): requirements.in
python3 -m venv .venv
$@ -m pip install --upgrade pip
$@ -m pip install pip-tools
@touch $@ # mark updated
requirements.txt: $(PYTHON) requirements.in
$(PYTHON) -m piptools compile -o $@ requirements.in
$(PYTHON) -m piptools sync
@touch $@ # mark updated
init: $(PYTHON) ## Create the virtual project environment
install: $(PYTHON) ## Install Jupyter kernel into user environment
$< -m ipykernel install --user --name my-project-name --display-name "My Project Name"
uninstall: ## Remove Jupyter kernel from user environment
jupyter kernelspec remove -f my-project-name || true
jupyter-notebook: install ## Run a Jupyter notebook server
jupyter notebook --debug --no-browser
jupyter-lab: install ## Run a Jupyter lab server
jupyter lab --debug --no-browser
clean: ## Remove generated files
clean-venv: uninstall ## Remove environment
rm -rf .venv
distclean: clean clean-venv ## Restore clean repository state
rm -f *.ipynb # if using jupytext
rm -f requirements.txt
rm -rf .ipynb_checkpointsNote that install becomes a prerequisite for make jupyter-notebook and make jupyter-lab, and uninstall becomes a prerequisite for make clean-venv.
Part of the python-localvenv-kernel wiki