sorin
sorin

Reputation: 170310

How to use make to create and refresh a python virtual environment only when needed?

I want to optimise the creation of and installation of python requirements.txt via pip inside a virtual environment via make.

The idea is that the pip install code should run only when the requirements.txt files were updated since the last execution.

I know that make is really smart regarding not recompiling tasks when the source files were not updated and in this case the source file would be the requirements.txt file.

How can I obtain this using make?

Upvotes: 2

Views: 802

Answers (1)

tripleee
tripleee

Reputation: 189297

Something like this?

.env: requirements.txt
    $(RM) -rf $@
    virtualenv $@ \
    && . ./$@/bin/activate \
    && pip install -r $<

As remarked by Etan Reisner (thanks!) the time stamp of the .env directory could change for other reasons, so you might want to use a flag file instead:

.env/made: requirements.txt
    $(RM) -rf $(@D)
    virtualenv $(@D) \
    && . ./$(@D)/bin/activate \
    && pip install -r $<
    touch $@

Upvotes: 4

Related Questions