Tox WARNING:test command found but not installed in testenv
Asked Answered
N

8

27

I am using tox for my project.

Here is my tox.ini file:

[tox]
envlist =
    py27,
    lint,
    coverage

skipsdist = True

[testenv:py27]
deps = -rrequirements.txt
commands = python -m unittest discover -s ./tests

[testenv:coverage]
commands =
    coverage run --source=tests -m unittest discover -s tests/
    coverage html
    coverage report


[testenv:lint]
commands = pylint ./foo

whenever I run tox, everything is getting executed which basically is linting, coverage.

but Tox is displaying warning for everything.

WARNING:test command found but not installed in testenv
Maybe you forgot to specify a dependency? See also the whitelist_externals envconfig setting.

Everything succeeds, but it is still displaying warning and errors. Can anyone tell me to what I am doing wrong?

My requirements.txt file is this:

requests==2.18.4
JsonForm==0.0.2
jsonify==0.5
jsonschema==2.6.0
JsonSir==0.0.2
python-dateutil==1.5
DateTime==4.2
urllib3==1.22
contextlib2==0.5.5
mock==2.0.0
patch==1.16
Newland answered 4/12, 2017 at 22:5 Comment(2)
Would u get the same error if you would configure pylint and coverage in your requirements?Tried
Just checked, yes it shows the same errors, but I am getting the desired output I want, but I am not able to understand why there are warnings for bothNewland
C
31

Programs that you use in commands must either be installed in tox' virtual environment or whitelisted:

[tox]
envlist =
    py27,
    lint,
    coverage

skipsdist = True

[testenv:py27]
deps = -rrequirements.txt
whitelist_externals = python
commands = python -m unittest discover -s ./tests

[testenv:coverage]
whitelist_externals = coverage
commands =
    coverage run --source=tests -m unittest discover -s tests/
    coverage html
    coverage report


[testenv:lint]
whitelist_externals = pylint
commands = pylint ./foo

Since tox>=4.0 this option has been renamed to allowlist_externals

Celadon answered 8/12, 2017 at 15:6 Comment(5)
Not sure why python should need to be whitelisted - it should be in the virtualenv! But I think this is the best answer and it should be accepted - @primer?Arvizu
In my case it was enough with whitelisting coverage with whitelist_externals = coverageVibration
@Vibration Depends on what programs you've installed in the virtualenv. If you've installed pylint you don't need to whitelist it.Celadon
Using whitelist_externals = python seems to defeat the point of tox, though, right, since your pyXY env would end up using whatever default system python instead of the version it was supposed to?Inwardly
For me on Ubuntu 22.04 with Python 3, I was getting an error with tox using the system python, /usr/bin/python, instead of the tox one, and then giving the whitelist error above. The same repo worked from a container, so there was something wrong with my system installation. I had to sudo apt reinstall python3-distutils to fix the problem.Phenothiazine
D
2

As specified by the error use:

whitelist_externals = <your command>

e.g. my external command is curl then tox.ini looks like

[tox]
envlist = py3
isolated_build = True

[testenv]
whitelist_externals = curl
deps =
    pytest
    chispa
    pyspark
    requests
    requests-mock
commands =
    curl url/hadoop-aws-3.2.0.jar --output ../lib/jars/hadoop-aws-3.2.0.jar
    curl url/aws-sdk-java-2.jar --output ../lib/jars/aws-sdk-java-2.jar
    python3 -m pytest
Diadem answered 19/8, 2021 at 12:19 Comment(0)
K
1

From the tox docs:

whitelist_externals has the same meaning and usage as allowlist_externals but it is now deprecated.


Since no one didn't mention it yet, I'll introduce allowlist_externals solution

Running tox in a virtual environment (python virtualenv) threw me warnings:

WARNING: test command found but not installed in testenv
Maybe you forgot to specify a dependency? See also the allowlist_externals envconfig setting.

The only command tox executed was commands = pytest --basetemp={envtmpdir}


Following the warning hint I went and check allowlist_externals which allows us to define which command can be used in the commands section without triggering "not installed in virtualenv" warnings


My final tox.ini [testenv] looked like this:

...
[testenv]
allowlist_externals = pytest
setenv =
    PYTHONPATH = {toxinidir}
deps =
    -r{toxinidir}\requirements_test_no_conflicts.txt
commands = 
    pytest --basetemp={envtmpdir}
...

As a side note: allowlist_externals can be used in different tox.ini sections

Koser answered 25/8, 2022 at 12:55 Comment(0)
S
0

https://tox.readthedocs.io/en/latest/config.html

here ,set this option,maybe,you pass

sitepackages=false(true|false) Set to true if you want to create virtual environments that also have access to globally installed packages.

Warning In cases where a command line tool is also installed globally you have to make sure that you use the tool installed in the virtualenv by using python -m (if supported by the tool) or {envbindir}/.

If you forget to do that you will get a warning like this:

WARNING: test command found but not installed in testenv
    cmd: /path/to/parent/interpreter/bin/<some command>
    env: /foo/bar/.tox/python
Maybe you forgot to specify a dependency? See also the whitelist_externals envconfig setting.
Squad answered 31/8, 2019 at 6:44 Comment(0)
C
0

I don't know why, but to solve I had to clone my repo again. The repo reset doesn't solve only the full clone.

Checkout this tox issue for more details.

Custommade answered 26/1, 2020 at 11:0 Comment(0)
G
0

This question is quite old, but I believe that there are common cases that are not best solved by the given answers. Namely, if tox is run on different machine than the development environment, for instance as part of CI/CD.


You don't need to allow commands that were added by tox. This can be done either by adding them as extra in your project configuration, or directly by using deps.

The direct option is easier to demonstrate. In your case it will be something like:

[testenv:coverage]
deps = coverage
commands =
    coverage run --source=tests -m unittest discover -s tests/
    coverage html
    coverage report

This solution is better then using allowlist_externals since if you run the code on different machine (in CI, for instance) then deps will install the needed library, while allowlist_externals (without deps) will use the pre-existing one (or fail if it doesn't exist).

As far as I know, you can add both options. However, if the allowlist_externals is required, something is probably not set correctly with your tox configuration.

Additional note: on rare instances, tox might miss that the package was installed by it. This happens to me (rarely) when I force stop tox via kill signal, remove/change files while tox is running, etc. In those (rare) cases, rebuilding the tox environment (via tox -r or removing .tox) usually solve the issue.

Gman answered 13/2 at 14:48 Comment(0)
I
-1

The question is old but the answers do not apply to my situation. In my case, once I changed the [testenv] to [env], it works. This is because my Python virtual environment is named as "env".

Interinsurance answered 22/10, 2019 at 16:7 Comment(1)
WARNING: The errors disappear but that is because it doesn't find the definition testenv any more. Renaming testenv to env is NOT the way to go! Your tests just won't be executed from that point onwards.Planck
S
-1

I solved the problem. The error pop up as a result of package dependencies that have been installed on your default virtual environment but not in the tox environment. Make sure that all the packages required to run tox is added in your requirements.txt including unittest and pytest

Soubriquet answered 27/9, 2021 at 7:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.