Makefile unable to run source command

Viewed 198

I'm trying to create a makefile for my machine learning project in python. Below is a sample code of it.

.PHONY: setup_env remove_env data features train predict run clean test
PROJECT_NAME=my-project

ifeq (,$(shell which pyenv))
    HAS_PYENV=False
    CONDA_ROOT=$(shell conda info --root)
    BINARIES = ${CONDA_ROOT}/envs/${PROJECT_NAME}/bin
else
    HAS_PYENV=True
    CONDA_VERSION=$(shell echo $(shell pyenv version | awk '{print $$1;}') | awk -F "/" '{print $$1}')
    BINARIES = $(HOME)/.pyenv/versions/${CONDA_VERSION}/envs/${PROJECT_NAME}/bin
endif

setup_env:
ifeq (True,$(HAS_PYENV))
    @echo ">>> Detected pyenv, setting pyenv version to ${CONDA_VERSION}"
    pyenv local ${CONDA_VERSION}
    conda env create --name $(PROJECT_NAME) -f environment.yaml --force
    pyenv local ${CONDA_VERSION}/envs/${PROJECT_NAME}
else
#   @echo ">>> Creating conda environment."
#   conda env create --name $(PROJECT_NAME) -f environment.yaml --force
    @echo ">>> Activating new conda environment"
    source $(CONDA_ROOT)/bin/activate $(PROJECT_NAME)
endif

The idea here is to create a conda environment from the yaml file and activate it. However, when I run this file in my terminal I'm getting the following error.

source /Users/myusername/opt/anaconda3/bin/activate my-project
make: source: No such file or directory
make: *** [setup_env] Error 1

Could you please help me fix this?

1 Answers

source is bash only. GNU Make runs sh (by default), not bash. The sh equivalent to bash source is ..

Change the offending line to this:

. $(CONDA_ROOT)/bin/activate $(PROJECT_NAME)
Related