Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 当requirements.txt或setup.py发生更改时,使用tox重新安装virtualenv_Python_Makefile_Virtualenv_Tox - Fatal编程技术网

Python 当requirements.txt或setup.py发生更改时,使用tox重新安装virtualenv

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

以前我手动使用的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'
# 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…