我需要从 Makefile 中激活 Python 的虚拟环境。
Python
venv
模块使用 3 个不同终端的激活文件创建虚拟环境:
是否可以从 Makefile 内部激活虚拟环境,而不知道人们将从什么类型的终端(CMD、PowerShell、bash)运行 Makefile 命令?
你可以在makefile中放入“if”条件,如果你有cmd、poweshell或bash执行某批命令,也许像这样:
# Detect the shell
ifeq ($(SHELL),/bin/bash)
SHELL_TYPE=bash
else ifeq ($(SHELL),/bin/sh)
SHELL_TYPE=sh
else ifeq ($(OS),Windows_NT)
SHELL_TYPE=cmd
else
SHELL_TYPE=unknown
endif
# Set up virtual environment
venv:
ifeq ($(SHELL_TYPE),bash)
source venv/bin/activate
else ifeq ($(SHELL_TYPE),cmd)
venv\Scripts\activate
else
@echo "Unsupported shell: $(SHELL_TYPE)"
@exit 1
endif
你可以在https://www.gnu.org/software/make/manual/html_node/Conditional-Syntax.html
中查看makefile使用以下命令,您可以检查您正在使用哪种类型的 shell。
echo $0
之后您可以简单地通过 if 语句捕获正确的路径。
https://askubuntu.com/questions/590899/how-do-i-check-which-shell-i-am-using