我正在和Anaconda一起使用Python,我希望在test
中有一个规则Makefile
。但是,我想声明在运行make test
时,将激活正确的环境。激活conda
环境时,它定义环境变量$(CONDA_DEFAULT_ENV)
。
首先我做了:
REPO_NAME = my_repo
define execute_in_env
source activate $(REPO_NAME); \
$1
endef
test:
$(call execute_in_env, pytest -v --ignore=src/foo)
这样,当我执行make test
时,它激活了虚拟环境并在里面运行测试。问题是它是长度。我想检查是否从激活的虚拟环境中执行了make
。为此,我修改了代码:
.PHONY: test
REPO_NAME = my_repo
define execute_in_env =
$@echo "Checking if in environment"
ifeq ($(CONDA_DEFAULT_ENV),$(REPO_NAME))
$1
else
source activate $(REPO_NAME); \
$1
endif
endef
test:
$(call execute_in_env, pytest -v --ignore=src/rebuydsutils)
通过这种实现,测试不会发生。我得到了
make: Nothing to be done for `test'.
答案 0 :(得分:1)
根问题在于=
中的define execute_in_env =
。 (你还在ifeq
里面使用define
,这有点尖锐,但在这种特殊情况下我不认为它会伤到你)。请注意,您正在使用不需要的功能 - 功能不易读取,并使makefile更难维护。做同样事情的一种更简单,更清洁的方法是:
ifeq ($(CONDA_DEFAULT_ENV),$(REPO_NAME))
ACTIVATE_ENV := source activate $(REPO_NAME);
else
ACTIVATE_ENV := true
endif
test:
$(ACTIVATE_ENV) && pytest -v --ignore=src/rebuydsutils;
答案 1 :(得分:0)
根据@ John的回答,我写了以下内容:
ifeq ($(CONDA_DEFAULT_ENV),$(REPO_NAME))
ACTIVATE_ENV := true
else
ACTIVATE_ENV := source activate $(REPO_NAME)
endif
# Execute python related functionalities from within the project's environment
define execute_in_env
$(ACTIVATE_ENV) && $1
endef
然后,在规则中我可以有类似的东西:
test:
$(call execute_in_env, pytest -v --ignore=src/somepackage)