Install python requirements.txt with Makefile only requirements.txt is changed
makefile, python
Solution
@Andrei.Danciuc, `make` just needs two files to compare; you can use any of the output files from running `pip install`.
For example, I usually use a "vendored" folder, so I can alias the path to the "vendored" folder instead of using a dummy file.
# Only run install if requirements.txt is newer than vendored folder
vendored-folder := vendored
.PHONY: install
install: $(vendored-folder)
$(vendored-folder): requirements.txt
rm -rf $(vendored-folder)
pip install -r requirements.txt -t $(vendored-folder)
If you don't use a vendored folder, this code below should work for both virtualenv and global setups.
# Only run install if requirements.txt is newer than SITE_PACKAGES location
.PHONY: install
SITE_PACKAGES := $(shell pip show pip | grep '^Location' | cut -f2 -d':')
install: $(SITE_PACKAGES)
$(SITE_PACKAGES): requirements.txt
pip install -r requirements.txt
Problem
How can I run target `make install` only if requirements.txt is changed ? I don't want to upgrade packages each time when I do `make install` I found some workaround by creating fake file `_requirements.txt.pyc` but is ugly and dirty. It will refuse install pip requirements second time because requirements.txt has no changes ``` $ make install-pip-requirements make: Nothing to be done for 'install-pip-requirements'. ``` But my goal is to do: ``` # first time, $ make install # create virtual environment, install requirements # second time $ make install # detected and skipping creating virtual env, # detect that requirements.txt have no changes # and skipping installing again all python packages make: Nothing to be done for 'install'. ``` Python package looks like: ``` . ├── Makefile ├── README.rst ├── lambda_handler.py └── requirements.txt ``` I am using file, `Makefile`, for some automation in python: ``` /opt/virtual_env: # create virtual env if folder not exists python -m venv /opt/virtual_env virtual: /opt/virtual_env # if requirements.txt is modified than execute pip install _requirements.txt.pyc: requirements.txt /opt/virtual_env/bin/pip install -r --upgrade requirements.txt echo > _requirements.txt.pyc requirements: SOME MAGIG OR SOME make flags pip install -r requirements.txt install-pip-requirements: _requirements.txt.pyc install: virtual requirements ``` I am sure that Must be a better way to do this;)