在requirements.txt或setup.py更改时,使用tox重新安装virtualenv

Ant*_*ile 11 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/activate && pip install -r requirements_dev.txt'
Run Code Online (Sandbox Code Playgroud)

这有一个很好的副作用,如果我更改了requirements_dev.txt或setup.py,它将重建我的virtualenv.但感觉有点笨重.

我想tox用来做类似的事情.我明白tox有一个--recreate选择,但我宁愿在我需要时才打电话.

我的新设置是这样的:

# Makefile
.PHONY: all
all: tests

.PHONY: tests
tests:
    tox
Run Code Online (Sandbox Code Playgroud)

# tox.ini
[tox]
project = my_project
envlist = py26,py27

[testenv]
install_command = pip install --use-wheel {opts} {packages}
deps = -rrequirements_dev.txt
commands =
    py.test {posargs:tests}
Run Code Online (Sandbox Code Playgroud)

一个理想的解决方案只会使用内容tox,但是可接受的解决方案将涉及Makefile和--recreate标志.

buk*_*zor 11

对于这个问题,tox似乎存在一个悬而未决的问题.

https://github.com/tox-dev/tox/issues/149(点击并添加您的评论和投票,让作者了解问题的常见程度)

我们需要提交补丁或解决它.想到的解决方法:

  1. 直接列出依赖关系tox.ini.使用您的构建系统确保tox.ini与.x保持同步requirements.txt.
  2. 将一个规则添加到Makefile中,该规则执行tox - 每当requirements.txt发生更改时都会重新创建.

解决方法2似乎最简单.


Ant*_*ile 5

这是我最终采用的 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
Run Code Online (Sandbox Code Playgroud)

例子:

$ make tests
touch .venv.touch
tox --recreate
[[ SNIP ]]
$ make tests
tox 
[[ SNIP ]]
$ touch requirements.txt
$ make tests
touch .venv.touch
tox --recreate
[[ SNIP ]]
Run Code Online (Sandbox Code Playgroud)