0

I'm writing a Makefile for my project (using MacOS). I have the following setup command:

init:
    python3 -m venv venv
    source venv/bin/activate
    pip install --upgrade pip
    pip install -r requirements.txt

This should create the virtual environment, activate it, and install required libraries.

However, when I run make init, I get:

python3 -m venv venv
source venv/bin/activate
make: source: No such file or directory
make: *** [init] Error 1

Running these commands manually works, so the Makefile can't see my venv directory for some reason. Any idea why?

I considered that it may be executing the second command before the first completes, but I believe the terminal should prevent that...

1 Answers1

0

Per Enis's linked answer in the comments, this solves my issue:

init:
    \
    python3 -m venv venv; \
    source venv/bin/activate; \
    pip install --upgrade pip; \
    pip install -r requirements.txt; \

The issue is that Makefile runs each line as a new shell, which prevents the virtual environment from being activated for subsequent commands.

My above code combines all the commands into a single line (from Make's perspective). Another option is to add the .ONESHELL: flag, although that affects the entire Makefile.