Reinstall virtualenv with tox when requirements.txt or setup.py changes

后端 未结 3 1521
北荒
北荒 2021-01-02 00:21

Previously I was manually using a Makefile that looked something like this:

.PHONY: all
all: tests

.PHONY: tests
tests: py_env
    bash -c \'source py_env/b         


        
相关标签:
3条回答
  • 2021-01-02 00:46

    Here's the Makefile workaround I ended up going with:

    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
    

    Example:

    $ make tests
    touch .venv.touch
    tox --recreate
    [[ SNIP ]]
    $ make tests
    tox 
    [[ SNIP ]]
    $ touch requirements.txt
    $ make tests
    touch .venv.touch
    tox --recreate
    [[ SNIP ]]
    
    0 讨论(0)
  • 2021-01-02 01:04

    There seems to be an open issue in tox for just this problem.

    https://github.com/tox-dev/tox/issues/149 (click and add your comment and vote, making the authors aware about how common the issue is)

    We'll need to either submit a patch or work around it. Workaround that come to mind:

    1. List dependencies directly in the tox.ini. Use your build system to ensure that the tox.ini stays in sync with the requirements.txt.
    2. Add a rule to your Makefile that does a tox --recreate whenever the requirements.txt changes.

    Workaround 2 seems most straightforward.

    0 讨论(0)
  • 2021-01-02 01:07

    Determined to solve this, I've written a tox plugin to accomplish this: https://github.com/asottile/tox-pip-extensions

    The plugin hooks into virtualenv creation and uses venv-update to keep dependencies in sync.

    The usage is pretty straightforward:

    • install tox-pip-extensions alongside tox (in my setup, I have a virtualenv at ~/venv with tox and tox-pip-extensions installed, and then symlinked ~/venv/bin/tox -> ~/bin/tox)
    • enable the extension as follows:

      [tox]
      tox_pip_extensions_ext_venv_update = true
      # the rest of your tox.ini file as normal...
      
    0 讨论(0)
提交回复
热议问题