Skip to content

Instantly share code, notes, and snippets.

@cowlicks
Created December 5, 2022 20:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cowlicks/3e3bfccfb7e8d1a3a2a40b87c7e8d7ec to your computer and use it in GitHub Desktop.
Save cowlicks/3e3bfccfb7e8d1a3a2a40b87c7e8d7ec to your computer and use it in GitHub Desktop.
Simple python project dependency management
PY_FILES := $(shell find . -name '*.py')
venv: venv/bin/activate
venv/bin/activate: requirements.txt
test -d venv || python3 -m venv venv
venv/bin/python -m ensurepip
venv/bin/python -m pip install -r requirements.txt
touch venv/bin/activate
test: venv
. venv/bin/activate && \
python -m unittest discover -v -f
check: venv
. venv/bin/activate && black -S --check $(shell git ls-files $(PY_FILES))
lint: venv
. venv/bin/activate && black -S $(shell git ls-files $(PY_FILES))

Use venv to create a directory called "venv" that holds all the dependencies and stuff

python -m venv venv

Activate th venv for this shell.

source venv/bin/activate

You can deacvate it with just

deacvate

With the venv active, just install things with pip

pip install numpy

When you add dependencies, update the requirements file with. Add this to git

pip freeze > requirements.txt

On a new machine, you will want to reacrate the venv with the requirements.txt file by combining the stuff above

python -m venv venv && source venv/bin/activate && pip install -r requirements.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment