Python 当requirements.txt或setup.py发生更改时,使用tox重新安装virtualenv
以前我手动使用的Makefile类似于:Python 当requirements.txt或setup.py发生更改时,使用tox重新安装virtualenv,python,makefile,virtualenv,tox,Python,Makefile,Virtualenv,Tox,以前我手动使用的Makefile类似于: .PHONY: all all: tests .PHONY: tests tests: py_env bash -c 'source py_env/bin/activate && py.test tests' py_env: requirements_dev.txt setup.py rm -rf py_env virtualenv py_env bash -c 'source py_env/bin/a
.PHONY: all
all: tests
.PHONY: tests
tests: py_env
bash -c 'source py_env/bin/activate && py.test tests'
py_env: requirements_dev.txt setup.py
rm -rf py_env
virtualenv py_env
bash -c 'source py_env/bin/activate && pip install -r requirements_dev.txt'
# Makefile
.PHONY: all
all: tests
.PHONY: tests
tests:
tox
这有一个很好的副作用,如果我更改了requirements_dev.txt或setup.py,它将重建我的virtualenv。但是感觉有点笨重
我想用tox
做类似的事情。我知道tox
有一个--重新创建选项,但我宁愿只在需要时才调用它
我的新设置如下所示:
.PHONY: all
all: tests
.PHONY: tests
tests: py_env
bash -c 'source py_env/bin/activate && py.test tests'
py_env: requirements_dev.txt setup.py
rm -rf py_env
virtualenv py_env
bash -c 'source py_env/bin/activate && pip install -r requirements_dev.txt'
# Makefile
.PHONY: all
all: tests
.PHONY: tests
tests:
tox
及
理想的解决方案应该只使用tox
中的内容,但是一个可接受的解决方案应该包括Makefile和--recreate
标志。tox中似乎有一个关于这个问题的未决问题
(单击并添加您的评论和投票,让作者了解该问题的普遍性)
我们需要提交补丁或解决它。想到的解决方法:
在tox.ini
中直接列出依赖项。使用构建系统确保tox.ini与requirements.txt
保持同步
在Makefile中添加一条规则,该规则在requirements.txt发生更改时执行tox--重新创建
变通方法2似乎最简单。以下是我最终采用的Makefile变通方法:
REBUILD_FLAG =
.PHONY: all
all: tests
.PHONY: tests
tests: .venv.touch
tox $(REBUILD_FLAG)
.venv.touch: setup.py requirements.txt requirements_dev.txt
$(eval REBUILD_FLAG := --recreate)
touch .venv.touch
例如:
$ make tests
touch .venv.touch
tox --recreate
[[ SNIP ]]
$ make tests
tox
[[ SNIP ]]
$ touch requirements.txt
$ make tests
touch .venv.touch
tox --recreate
[[ SNIP ]]
我也想要这个答案。这是一个非常有创意的使用make
的方法,好主意。写此评论只是为了指出,对于shell脚本,直接使用venv
中的命令要容易得多:py_env/bin/pip…
而不是bash-c'source py_env/bin/activate&&pip…