我想在处理指定的virtualenv时执行几个操作。
例如命令
make install
相当于
source path/to/virtualenv/bin/activate
pip install -r requirements.txt
可能吗?
在make中,您可以运行shell作为命令。在这个shell中,您可以在从comandline开始的shell中执行所有操作。例:
install:
( \
source path/to/virtualenv/bin/activate; \
pip install -r requirements.txt; \
)
必须注意;
and和\
。
打开和关闭括号之间的所有内容都将在shell的单个实例中完成。
我喜欢使用只在requirements.txt
改变时运行的东西:
venv: venv/bin/activate
venv/bin/activate: requirements.txt
test -d venv || virtualenv venv
. venv/bin/activate; pip install -Ur requirements.txt
touch venv/bin/activate
test: venv
. venv/bin/activate; nosetests project/test
clean:
rm -rf venv
find -iname "*.pyc" -delete
我很幸运。
install:
source ./path/to/bin/activate; \
pip install -r requirements.txt; \
您还可以使用名为“VIRTUALENVWRAPPER_SCRIPT”的环境变量。像这样:
install:
( \
source $$VIRTUALENVWRAPPER_SCRIPT; \
pip install -r requirements.txt; \
)
通常,make
在不同的子shell中运行配方中的每个命令。但是,设置.ONESHELL:
将在同一个子shell中运行配方中的所有命令,允许您激活virtualenv然后在其中运行命令。
请注意,.ONESHELL:
适用于整个Makefile,而不仅仅是单个配方。它可能会更改现有命令的行为,完整文档中可能出现的错误的详细信息。这不会让你激活virtualenv以便在Makefile之外使用,因为命令仍在子shell中运行。
参考文档:https://www.gnu.org/software/make/manual/html_node/One-Shell.html
例:
.ONESHELL:
.PHONY: install
install:
source path/to/virtualenv/bin/activate
pip install -r requirements.txt
这是运行您想要在virtualenv中运行的东西的另一种方法。
BIN=venv/bin/
install:
$(BIN)pip install -r requirements.txt
run:
$(BIN)python main.py
PS:这不会激活virtualenv,但会完成任务。希望你发现它干净而有用。
你应该使用它,它现在对我有用。
report.ipynb : merged.ipynb
( bash -c "source ${HOME}/anaconda3/bin/activate py27; which -a python; \
jupyter nbconvert \
--to notebook \
--ExecutePreprocessor.kernel_name=python2 \
--ExecutePreprocessor.timeout=3000 \
--execute merged.ipynb \
--output=$< $<" )