如何在Makefile中激活virtualenv?
我试过了:
venv:
@virtualenv venv
active:
@source venv/bin/activate
Run Code Online (Sandbox Code Playgroud)
我也尝试过:
active:
@. venv/bin/activate
Run Code Online (Sandbox Code Playgroud)
并且它不会激活virtualenv.
vik*_*kho 14
UPD 2021 年 3 月 14 日
virtualenv 内部的另一种变体pip install:
# Makefile
all: install run
install: venv
: # Activate venv and install smthing inside
. venv/bin/activate && pip install -r requirements.txt
: # Other commands here
venv:
: # Create venv if it doesn't exist
: # test -d venv || virtualenv -p python3 --no-site-packages venv
test -d venv || python3 -m venv venv
run:
: # Run your app here, e.g
: # determine if we are in venv,
: # see /sf/ask/131008461/
. venv/bin/activate && pip -V
: # Or see @wizurd's answer to exec multiple commands
. venv/bin/activate && (\
python3 -c 'import sys; print(sys.prefix)'; \
pip3 -V \
)
clean:
rm -rf venv
find -iname "*.pyc" -delete
Run Code Online (Sandbox Code Playgroud)
因此,您可以make使用不同的“标准”方式运行:
make- 目标为默认值allmake venv- 只需创建 virtualenvmake install- 制作 venv 并执行其他命令make run- 在 venv 中执行您的应用程序make clean- 删除 venv 和 python 二进制文件方法如下:
您可以使用()在Makefile中执行shell命令;
例如
echoTarget:
(echo "I'm an echo")
Run Code Online (Sandbox Code Playgroud)
只要确保在shell命令的每一行之前放置一个制表符即可。也就是说,您之前需要一个选项卡(回声“我是回声”)
以下是激活virtualenv的方法:
activate:
( \
source path/to/virtualenv/activate; \
pip install -r requirements.txt; \
)
Run Code Online (Sandbox Code Playgroud)