You might find @falsetru's answer doesn't work if you installed pytest system wide AFTER you installed in in the virtualenv, as I did.
(Quick note: py.test is the old version of the command, pytest is the recommended way, but what applies to one applies to the other)
You can find out where pytest is being loaded from by calling which pytest
.
As you can see, my pytest points one that is not in the virtualenv:
(env) ~/projects/test/website $ which python
/home/andrew/projects/test/website/env/bin/python
(env) ~/projects/test/website $ which pytest
/home/andrew/.local/bin/pytest
Here's proof that pytest IS installed in the virtualenv:
(env) ~/projects/test/website $ pip install pytest
Requirement already satisfied: pytest in ./env/lib/python3.5/site-packages
Requirement already satisfied: py>=1.4.29 in ./env/lib/python3.5/site-packages (from pytest)
And hash -r
makes no difference (as no new installation happened):
(env) ~/projects/test/website $ hash -r
(env) ~/projects/test/website $ which pytest
/home/andrew/.local/bin/pytest
The solution is to uninstall and reinstall pytest in the virtualenv,
(env) ~/projects/test/website $ pip uninstall pytest
...
(env) ~/projects/test/website $ pip install pytest
...
(env) ~/projects/test/website $ which pytest
/home/andrew/projects/test/website/env/bin/pytest
If for some reason you can't reinstall, one way to get round this is by calling pytest as a module via the python in your virtualenv:
python -m pytest