Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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/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'

This had the nice side-effect that if I changed requirements_dev.txt or setup.py, it would rebuild my virtualenv. But feels a bit clunky.

I'd like to use tox to do a similar thing. I understand tox has a --recreate option, but I'd rather call that only when I need to.

My new setup is something like this:

# Makefile
.PHONY: all
all: tests

.PHONY: tests
tests:
    tox

and

# 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}

An ideal solution would use just things in tox, however an acceptable solution would involve the Makefile and the --recreate flag.

like image 274
Anthony Sottile Avatar asked Apr 12 '14 15:04

Anthony Sottile


People also ask

Does tox use Virtualenv?

tox is a generic virtualenv management and test command line tool you can use for: checking that your package installs correctly with different Python versions and interpreters. running your tests in each of the environments, configuring your test tool of choice.

What is tox file in Python?

tox is a command-line driven automated testing tool for Python, based on the use of virtualenv . It can be used for both manually-invoked testing from the desktop, or continuous testing within continuous integration frameworks such as Jenkins or Travis CI.

How does Python tox work?

Tox will automatically use the right version of the interpreter, based on the version of the environment, to create the virtual environment. Tox will automatically rebuild the virtual environment if it is missing or if the dependencies change. It is possible to explicitly indicate the Python version in an environment.

How do I run a tox INI file?

# content of: tox. ini , put in same dir as setup.py [tox] envlist = py26,py27 [testenv] deps=pytest # install pytest in the venvs commands=pytest # or 'nosetests' or ... You can also try generating a tox. ini file automatically, by running tox-quickstart and then answering a few simple questions.


2 Answers

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.

like image 188
bukzor Avatar answered Sep 23 '22 05:09

bukzor


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 ]]
like image 24
Anthony Sottile Avatar answered Sep 24 '22 05:09

Anthony Sottile