Running coverage inside virtualenv
Asked Answered
R

2

23

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.

Rost answered 23/9, 2013 at 12:18 Comment(3)
I can't reproduce this on windows, I'm afraid. Coverage works fine. + 1 to help a fellow pythonista in need :)Dissimilar
Did you try to install the coverage package in the virtualenv you are creating?Aromatic
@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".Rost
C
17

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 ~]$
Crymotherapy answered 23/9, 2013 at 13:5 Comment(2)
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!Rost
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.Molecule
V
27

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
Ventricle answered 29/4, 2014 at 23:39 Comment(4)
I am sorry, that isn't an answer, that's a bug report for the virtualenv maintainer.Crymotherapy
Not sure why you got a downvote for this, its exactly what was causing my problem. Upvotes all around.Whomp
@Crymotherapy it is an answer, and it's still valid today - just fixed my issue in Python 3Halvorsen
@Halvorsen I was unclear 4 years ago... Its not an answer to the question asked.Crymotherapy
C
17

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 ~]$
Crymotherapy answered 23/9, 2013 at 13:5 Comment(2)
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!Rost
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.Molecule

© 2022 - 2024 — McMap. All rights reserved.