gpt4 book ai didi

python - 检查 Makefile 中是否存在 conda 环境

转载 作者:行者123 更新时间:2023-12-01 06:26:34 27 4
gpt4 key购买 nike

我正在 Makefile 中创建 conda 环境。但是,如果已经创建了此环境,我想跳过该步骤。我如何检查以下内容:

  1. 是否已有同名的环境?简单的目录检查就足够了吗?
  2. 此环境是否已激活?选修的。我认为,如果 env 被激活两次也没什么坏处。
CONDA_ENV_NAME := myname

ifeq (,$(shell which conda))
HAS_CONDA=False
else
HAS_CONDA=True
CONDA_ACTIVATE=source $$(conda info --base)/etc/profile.d/conda.sh ; conda activate ; conda activate
endif

environment:
ifeq (True,$(HAS_CONDA))
@echo ">>> Detected conda, creating conda environment."
## Here I'd like to check if this environment already exists
conda env create -f environment.yml -n $(CONDA_ENV_NAME)
## ... and if the env should be activated (optional)
$(CONDA_ACTIVATE) $(CONDA_ENV_NAME)
else
@echo ">>> Install conda first."
endif


最佳答案

我通过以下方法解决了问题:

ifeq (,$(shell which conda))
HAS_CONDA=False
else
HAS_CONDA=True
ENV_DIR=$(shell conda info --base)
MY_ENV_DIR=$(ENV_DIR)/envs/$(CONDA_ENV_NAME)
CONDA_ACTIVATE=source $$(conda info --base)/etc/profile.d/conda.sh ; conda activate ; conda activate
endif

environment:
ifeq (True,$(HAS_CONDA))
ifneq ("$(wildcard $(MY_ENV_DIR))","") # check if the directory is there
@echo ">>> Found $(CONDA_ENV_NAME) environment in $(MY_ENV_DIR). Skipping installation..."
else
@echo ">>> Detected conda, but $(CONDA_ENV_NAME) is missing in $(ENV_DIR). Installing ..."
conda env create -f environment.yml -n $(CONDA_ENV_NAME)
endif
else
@echo ">>> Install conda first."
exit
endif

关于python - 检查 Makefile 中是否存在 conda 环境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60115420/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com