如何在不认识终端的情况下激活Python虚拟环境

问题描述 投票:0回答:2

我需要从 Makefile 中激活 Python 的虚拟环境。

Python

venv
模块使用 3 个不同终端的激活文件创建虚拟环境:

  • 激活
  • 激活.bat
  • 激活.ps1

是否可以从 Makefile 内部激活虚拟环境,而不知道人们将从什么类型的终端(CMD、PowerShell、bash)运行 Makefile 命令?

python makefile terminal virtualenv
2个回答
0
投票

你可以在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

0
投票

使用以下命令,您可以检查您正在使用哪种类型的 shell。

echo $0

之后您可以简单地通过 if 语句捕获正确的路径。

https://askubuntu.com/questions/590899/how-do-i-check-which-shell-i-am-using

© www.soinside.com 2019 - 2024. All rights reserved.