21

I recently stumbled upon some issue with running coverage measurements within virtual environment. I do not remember similar issues in the past, nor I was able to find solution on the web.

Basically, when I am trying to run test suite in virtualenv, it works fine. But as soon, as I try to do it using coverage, it fails because of lack of modules it requires. Based on some answer on StackOverflow I checked my script and found out that coverage uses different interpreter, even if running from inside the same virtualenv.

Here is how to reproduce it:

$ virtualenv --no-site-packages venv
New python executable in venv/bin/python
Installing Setuptools................................................done.
Installing Pip.......................................................done.
$ source venv/bin/activate
(venv)$ echo 'import sys; print(sys.executable)' > test.py
(venv)$ python test.py
/home/tadeck/testground/venv/bin/python
(venv)$ coverage run test.py 
/usr/bin/python

The question is: how to make coverage work with virtual environment seamlessly? I could alter sys.path or install required modules system-wide, but there has to be a cleaner way.

oz123
  • 27,559
  • 27
  • 125
  • 187
Tadeck
  • 132,510
  • 28
  • 152
  • 198
  • I can't reproduce this on windows, I'm afraid. Coverage works fine. + 1 to help a fellow pythonista in need :) – Games Brainiac Sep 23 '13 at 12:44
  • 1
    Did you try to install the ``coverage`` package in the virtualenv you are creating? – fjarri Sep 23 '13 at 12:48
  • @Bogdan: It was already there, so I did not try to (re)install it. But when I do `pip install -U coverage`, I get "`Requirement already up-to-date: coverage in ./venv/lib/python2.7/site-packages`". – Tadeck Sep 23 '13 at 15:22

2 Answers2

25

I had to leave my virtualenv after installing coverage and reactivate it to get coverage to work.

[alex@gesa ~]$ virtualenv --no-site-packages venv
[alex@gesa ~]$ source venv/bin/activate
(venv)[alex@gesa ~]$ pip install coverage
(venv)[alex@gesa ~]$ deactivate
[alex@gesa ~]$ source venv/bin/activate
Dan Hooper
  • 1,077
  • 8
  • 6
15

pip install coverage in your new venv

[alex@gesa ~]$ virtualenv venv
[alex@gesa ~]$ source venv/bin/activate
(venv)[alex@gesa ~]$ pip install coverage
(venv)[alex@gesa ~]$ echo 'import sys; print(sys.executable)' > test.py
(venv)[alex@gesa ~]$ python test.py
/home/alex/venv/bin/python
(venv)[alex@gesa ~]$ coverage run test.py
/home/alex/venv/bin/python
(venv)[alex@gesa ~]$
Tritium21
  • 2,845
  • 18
  • 27
  • Your solution helped - I reinstalled `coverage` inside virtualenv. It was installed, but somehow the command was overwritten nad was pointing to wrong place. Now it works properly. Thank you! – Tadeck Sep 23 '13 at 18:14
  • 1
    This worked for me as well. I figured having coverage installed globally was enough, but it kept throwing import errors related to the packages installed in my virtual environment, even though I had the virtual environment activated when running coverage... It seems like coverage should support this (common) use case. – Taylor D. Edmiston Apr 22 '15 at 02:26