24

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
peterh
  • 11,875
  • 18
  • 85
  • 108
primer
  • 381
  • 1
  • 2
  • 7
  • Would u get the same error if you would configure pylint and coverage in your requirements? – alecxe Dec 04 '17 at 22:24
  • 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 both – primer Dec 04 '17 at 22:29

7 Answers7

30

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
phd
  • 82,685
  • 13
  • 120
  • 165
  • 1
    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? – qris Mar 18 '21 at 21:06
  • In my case it was enough with whitelisting coverage with `whitelist_externals = coverage` – Duilio Jul 13 '21 at 12:55
  • 1
    @Duilio Depends on what programs you've installed in the virtualenv. If you've installed `pylint` you don't need to whitelist it. – phd Jul 13 '21 at 12:59
  • 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? – solsword Apr 14 '22 at 15:40
  • 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. – iinuwa Aug 17 '22 at 14:51
1

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
s510
  • 2,271
  • 11
  • 18
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

Jack
  • 31
  • 5
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.
betontalpfa
  • 3,454
  • 1
  • 33
  • 65
sunjiyun
  • 9
  • 1
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.

betontalpfa
  • 3,454
  • 1
  • 33
  • 65
-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".

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Garry
  • 15
  • 1
  • 4
    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. – h345k34cr Sep 17 '20 at 09:19
-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