0

I'm running some commands through a Makefile and I have to create a new python virtualenv, activate it and install requirements in one Make recipe/goal and then reuse it before running other recipes/goals, but the problem is, I have to activate that env on every subsequent Make Goal like so

    SHELL := bash
# For Lack of a better mechanism will have to activate the venv on every Make recipe because every step is in its own shell

.ONESHELL:
install:
    python -m venv .venv
    source .venv/bin/activate
    pip install -r requirements.txt

test:   install
    source .venv/bin/activate
    pytest

synth:  install
    source .venv/bin/activate
    cdk synth --no-color

diff:   install
    source .venv/bin/activate
    cdk diff --no-color

bootstrap:  install
    source .venv/bin/activate
    cdk bootstrap --no-color

deploy: install
    source .venv/bin/activate
    cdk deploy --no-color


.PHONY: install test    synth   diff    bootstrap   deploy

Is there a better way to do this, i.e saves me having to do source .venv/bin/activate on every single goal ? In other words can I run all make goals in a Makefile in the same SHELL basically ?

1 Answer 1

2

You cannot run all goals in the same shell (that cannot work, since the shell must exit after each recipe is complete else make cannot know whether the commands in the recipe were successful or not, and hence whether the rule was successful or not).

You can of course put the sourcing in a variable so you don't have to type it out. You could also put the entire thing in a function, like this:

run = . .venv/bin/activate && $1

then:

install:
        python -m venv .venv
        $(call run,pip install -r requirements.txt)

test:   install
        $(call run,pytest)

etc.

If you don't want to do any of that your only choice is to use recursive make invocations. However this is going to be tricky because you only want to do it after the install rule is created. Not sure it will actually lead to a more understandable makefile.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.