Skip to content Skip to sidebar Skip to footer

How To Clean A Tox Environment After Running?

I have the following tox.ini file: [tox] envlist = flake8,py{35,36,37,38}{,-keyring} [testenv] usedevelop = True install_command = pip install -U {opts} {packages} deps = .[te

Solution 1:

I know it's not exactly what you were asking for but it's worth mentioning that the -r / --recreate flag to tox will force recreation of virtual environments

Solution 2:

There is no way in tox. The reason is that tox preserves these environments as a cache: next time you run tox the environments will be reused thus saving time.

You can remove them at once after running tox with rm -rf .tox.

Solution 3:

I found a way by creating a tox hook. This hook runs the shutil.rmtree command after the tests have been run inside the env.

In a tox_clean_env.py file:

import shutil
from tox import hookimpl

@hookimpldeftox_runtest_post(venv):
    try:
        shutil.rmtree(venv.path)
    except Exception as e:
        print("An exception occurred while removing '{}':".format(venv.path))
        print(e)

I created a package around this code and I just need to install it using pip.

In my setup.py, in the setup function:

entry_points={"tox": ["clean_env = tox_clean_env"]},

Post a Comment for "How To Clean A Tox Environment After Running?"