Skip to content

Instantly share code, notes, and snippets.

@Olshansk
Created July 12, 2023 04:27
Show Gist options
  • Save Olshansk/14527c9bc1be3fa8d54d33f59df150ee to your computer and use it in GitHub Desktop.
Save Olshansk/14527c9bc1be3fa8d54d33f59df150ee to your computer and use it in GitHub Desktop.
PyMakefileStarter: Python Project Makefile Foundation
SHELL := /bin/bash
.SILENT:
.PHONY: help
.DEFAULT_GOAL := help
help: ## Prints all the targets in all the Makefiles
@grep -h -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
.PHONY: list
list: ## List all make targets
@${MAKE} -pRrn : -f $(MAKEFILE_LIST) 2>/dev/null | awk -v RS= -F: '/^# File/,/^# Finished Make data base/ {if ($$1 !~ "^[#.]") {print $$1}}' | egrep -v -e '^[^[:alnum:]]' -e '^$@$$' | sort
##########################
### Env Common Targets ###
##########################
.PHONY: check-env
check-env: ## Checks if the virtual environment is activated
ifndef VIRTUAL_ENV
$(error 'Virtualenv is not activated, please activate the Python virtual environment by running "$$(make env_source)".')
endif
.PHONY: env_create
env_create: ## Create the env; must be execute like so: $(make env_create)
python3 -m venv venv
.PHONY: env_source
env_source: ## Source the env; must be execute like so: $(make env_source)
@echo 'source venv/bin/activate'
##########################
### Pip Common Targets ###
##########################
.PHONY: pip_freeze
pip_freeze: check-env ## Freeze the pip requirements
pip freeze > requirements.txt
.PHONY: pip_install
pip_install: check-env ## Install the pip requirements
pip install -r requirements.txt
#############################
### Python Common Targets ###
#############################
.PHONY: py_format
py_format: check-env ## Format the python code
black .
isort .
####################
### Your stuff ###
####################
@Olshansk
Copy link
Author

Add this to your README:


## Getting Started

### Environment Setup (One Time)

\```bash
    make env_create
    $(make env_source)
    make pip_install
\```

### Environment Usage (Every Time)

\```bash
    $(make env_source)
    # If new dependencies were added
    make pip_freeze
\```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment