pythoncondatox

tox-conda: multiple Python interpreters for tests


All afternoon I've been reading and rereading documentation for tox and tox-conda, lots of blog and Stack Overflow posts, and I still haven't a clue how to make tox use Conda to test my Python package against multiple Python interpreter versions.

I'm an Earth scientist; I use Miniforge to manage my Python environments because it handles non-Python dependencies (and, for that matter, R projects) nicely. So, I start off by creating a virtual environment to contain the tox/tox-conda setup:

mamba create -n sandbox python=3.10 tox-conda
mamba activate sandbox

Then I create Conda environments to give tox access to Python 3.10 an 3.11:

mamba create -n py311 python=3.11
mamba create -n py310 python=3.10

Then I try to test my package against 3.10 and 3.11 by running tox in my Python package directory with this tox.ini file:

[tox]
requires = tox-conda
envlist = py310, py311
isolated_build = True

[testenv]
deps = pytest
commands =
         pytest

It works like a charm for 3.10. I think, because my base environment for tox uses 3.10. tox can't find the interpreter for 3.11, and I can't work out what to put in tox.ini to point it there. The error I get is ERROR: cowardly refusing to delete envdir (it does not look like a virtualenv): /home/timh/Code/pgongrid_sandbox/.tox/py311

I've tried adding [testenv:py310] and [testenv:py311] sections to tox.ini that use the absolute path to the interpreters (e.g., $HOME/mambaforge/envs/py311/bin/python3) or specify python=3.10 or python=3.11 in the deps or conda-deps settings, but I've not managed to steer tox away from the Python available in the Conda environment I run tox from.

Is there an example tox.ini anywhere on the web that does this? None of the settings in the tox-conda Usage documentation seem to fit the bill.

This is on Ubuntu 20.04 (Focal Fossa), tox 3.28.0, tox-conda 0.10.2, mamba 1.5.2, and Conda 23.9.0.


Solution

  • Use the conda-deps setting in your tox.ini file. This setting allows you to specify a list of conda packages that should be installed in the Tox environment. You can use this to install the Python interpreter that you want to use for the tests. For example, the following tox.ini file will create two Tox environments, one for Python 3.10 and one for Python 3.11. The Python interpreter for each environment will be installed from Conda.

    [tox]
    requires = tox-conda
    envlist = py310, py311
    
    [testenv]
    conda-deps =
        python=3.10
        pytest
    
    [testenv:py311]
    conda-deps =
        python=3.11
        pytest