87

I installed pytest into a virtual environment (using virtualenv) and am running it from that virtual environment, but it is not using the packages that I installed in that virtual environment. Instead, it is using the main system packages. (Using python -m unittest discover, I can actually run my tests with the right python and packages, but I want to use the py.test framework.)

Is it possible that py.test is actually not running the pytest inside the virtual environment and I have to specify which pytest to run?

How to I get py.test to use only the python and packages that are in my virtualenv?

Also, since I have several version of Python on my system, how do I tell which Python that Pytest is using? Will it automatically use the Python within my virtual environment, or do I have to specify somehow?

LightCC
  • 9,804
  • 5
  • 52
  • 92
Henry Grantham
  • 1,025
  • 2
  • 8
  • 11
  • 1
    Did you actually activate the virtualenv you created before installing and running pytest? i.e. `source venv/bin/activate` – Filip Jukić Jan 27 '16 at 18:31
  • 1
    Definitely. I use virtualenvwrapper and the currently activated virtualenv shows up on my prompt when it is activated. Also, I can see the current virtualenv's installed packages using pip freeze. – Henry Grantham Jan 27 '16 at 18:49
  • 8
    I got this to work. It just required a terminal restart. – Henry Grantham Jan 27 '16 at 21:42

4 Answers4

126

There is a bit of a dance to get this to work:

  1. activate your venv : source venv/bin/activate
  2. install pytest : pip install pytest
  3. re-activate your venv: deactivate && source venv/bin/activate

The reason is that the path to pytest is set by the sourceing the activate file only after pytest is actually installed in the venv. You can't set the path to something before it is installed.

Re-activateing is required for any console entry points installed within your virtual environment.

7yl4r
  • 4,788
  • 4
  • 34
  • 46
82

Inside your environment, you may try

python -m pytest
Icarus
  • 1,335
  • 1
  • 11
  • 24
3

In my case I was obliged to leave the venv (deactivate), remove pytest (pip uninstall pytest), enter the venv (source /my/path/to/venv), and then reinstall pytest (pip install pytest). I don't known exacttly why pip refuse to install pytest in venv (it says it already present).

I hope this helps

jmcollin92
  • 2,896
  • 6
  • 27
  • 49
  • I had pytest in my base env, but not in my venv so when I called `py.test -v` in the venv it was using the pytest _outside_ the venv and not finding my packages. Anyway, just activating the venv and then doing `conda install pytest` worked for me. (pip works too, I assume.) – seth127 Sep 24 '18 at 19:59
  • 1
    It sounds to me like you had `pytest` installed in both global & venv, but the path to `pytest` had not been updated in your terminal. I think removing the global was not necessary; you just needed to re-`source` the activate script after `pytest` was installed in the `venv` (see my answer). – 7yl4r Feb 08 '19 at 17:26
0

you have to activate your python env every time you want to run your python script, you have several ways to activate it, we assume that your virtualenv is installed under /home/venv :

1- the based one is to run the python with one command line >>> /home/venv/bin/python <your python file.py>

2- add this line on the top of python script file #! /home/venv/bin/python and then run python <you python file.py>

3- activate your python env source /home/venv/bin/activate and then run you script like python <you python file.py>

4- use virtualenvwrapper to manager and activate your python environments