Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Testing Covering Your Bases Using Coverage

I can't use Coverage in my workspace

I tried to use Coverage but I got the following error :

treehouse:~/workspace/dice$ coverage run tests.py
-bash: /home/treehouse/.local/bin/coverage: /usr/local/pyenv/versions/3.4.1/bin/python3.4: bad interpreter: No such file or directory

Notes :

  • I added the following to tests.py : if name == 'main': unittest.main()

  • I also tried to install Coverage on my workspace : treehouse:~/workspace/dice$ pip install coverage
    Requirement already satisfied (use --upgrade to upgrade): coverage in /usr/local/pyenv/versions/3.5.0/lib/python3.5/site-packages
    pyenv: cannot rehash: /usr/local/pyenv/shims isn't writable

Is there anything I didn't do correctly?

11 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

Michaël and Josh, You rang....

It appears that the environment has changed (Python is now 3.5) within the workspaces but the coverage scripts have not been updated (expects Python 3.4.1). I was able to hack this workaround in the workspaces:

# Change to dice directory
treehouse:~/workspace$ cd dice
# Explore dir
treehouse:~/workspace/dice$ ls
__pycache__  dice.py  tests.py
# Try to run 'coverage'
treehouse:~/workspace/dice$ coverage tests.py
-bash: /home/treehouse/.local/bin/coverage: /usr/local/pyenv/versions/3.4.1/
bin/python3.4: bad interpreter: No such file or directory
# Fails! Time to debug....

# Where is coverage
treehouse:~/workspace/dice$ which coverage
~/.local/bin/coverage

# What is the first line of file
treehouse:~/workspace/dice$ head -1 `!!`
head -1 `which coverage`
#!/usr/local/pyenv/versions/3.4.1/bin/python3.4

# Coverage expects Python 3.4.1. Let's see what's in the workspace
treehouse:~/workspace/dice$ ls /usr/local/pyenv/versions
3.5.0
treehouse:~/workspace/dice$ ls /usr/local/pyenv/versions/3.5.0/bin/python3.5
/usr/local/pyenv/versions/3.5.0/bin/python3.5

# Need to change the `coverage` script to point to this version.

# pushd directory to 'coverage' dir
treehouse:~/workspace/dice$ pushd ~/.local/bin/
~/.local/bin ~/workspace/dice

# make a back up of coverage
treehouse:~/.local/bin$ cp coverage{,.back}
treehouse:~/.local/bin$ ls
coverage  coverage-3.4  coverage.back  coverage3

# Edit file to change first line from:
#!/usr/local/pyenv/versions/3.4.1/bin/python3.4
# to:
#!/usr/local/pyenv/versions/3.5.0/bin/python3.5
treehouse:~/.local/bin$ vi coverage

# Verify change
treehouse:~/.local/bin$ head -1 coverage
#!/usr/local/pyenv/versions/3.5.0/bin/python3.5

# Change back to dice directory
treehouse:~/workspace/dice$ popd
~/workspace/dice

# Try to run 'coverage'
treehouse:~/workspace/dice$ coverage run tests.py
.......
----------------------------------------------------------------------
Ran 7 tests in 0.003s

OK
# Success!!!

# Run coverage for report
treehouse:~/workspace/dice$ coverage report
Name    Stmts   Miss  Cover
---------------------------
dice       50      9    82%
tests      33      0   100%
---------------------------
TOTAL      83      9    89%
treehouse:~/workspace/dice$

Tagging Kenneth Love for escalation to correct the coverage script in workspaces (and collect a bug bounty!)

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

There are some reporting this issue is back. Here is yet another solution: (copying answer from here)

There may be an workspaces environment error. The issue is there are two coverage executables. In the older workspaces the directory ~/.local/bin is ahead of the /usr/local/pyenv/shims. The version in the ~/.local/bin is old and can be removed.

To create a workaround:

# check currently run executable location
treehouse:~/workspace$ which coverage
~/.local/bin/coverage

# rename coverage
treehouse:~/workspace$ cd ~/.local/bin
treehouse:~/.local/bin$ ls
coverage  coverage3  coverage-3.4
treehouse:~/.local/bin$ mv coverage coverage.old


# recheck currently run executable location
treehouse:~/.local/bin$ which coverage
/usr/local/pyenv/shims/coverage

# rehash bash executable locations
treehouse:~/.local/bin$ hash -r

# test run coverage
treehouse:~/.local/bin$ coverage --version
Coverage.py, version 4.0.3.
Documentation at https://coverage.readthedocs.org
Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Hmm. Good catch. I'll send this along to our Workspaces team. In the meantime, though, couldn't you do pip install coverage or maybe pip install --upgrade coverage?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

The --upgrade appears to do the trick:

treehouse:~/workspace$ pip install --upgrade coverage 
Collecting coverage 
  Downloading coverage-4.0.3.tar.gz (354kB) 
    100% |████████████████████████████████| 356kB 843kB/s 
Installing collected packages: coverage               
  Running setup.py install for coverage 
Successfully installed coverage-3.7.1 
pyenv: cannot rehash: /usr/local/pyenv/shims isn't writable 
treehouse:~/workspace/dice$ cd ~/workspace/dice 
treehouse:~/workspace/dice$ coverage run tests.py
....... 
----------------------------------------------------------------------
Ran 7 tests in 0.003s 

OK 
treehouse:~/workspace/dice$ coverage report 
Name       Stmts   Miss  Cover 
------------------------------  
dice.py       50      9    82% 
tests.py      33      0   100% 
------------------------------ 
TOTAL         83      9    89%
Josh Keenan
Josh Keenan
19,652 Points

I always got the same problem and was unable to find a fix, perhaps Chris Freeman can help you :), he can solve pretty much any problem.

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Hmm. Good catch. I'll send this along to our Workspaces team. In the meantime, though, couldn't you do pip install coverage or maybe pip install --upgrade coverage?

Thanks a lot!

Theo Lekkas
Theo Lekkas
5,908 Points

Hi all. This stills seems to be a problem. I encountered the problem as described by the OP. I initially attempted to pip upgrade then attempted Chris Freeman's workaround, I still got the original error. I then attempted to pip upgrade again, no luck. Let me know if you need more info.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

In your workspace can you run the following commands:

treehouse:~/workspace$ head -1 ~/.local/bin/coverage
#!/usr/local/pyenv/versions/3.5.0/bin/python3.5
treehouse:~/workspace$ python --version
Python 3.5.0

And post your results? Thanks.

Theo Lekkas
Theo Lekkas
5,908 Points

Apparently the .local dir is empty

treehouse:~/workspace$ head -1 ~/.local/bin/coverage
head: cannot open ‘/home/treehouse/.local/bin/coverage’ for reading: No such file or directory
treehouse:~/workspace$ ls ~/.local/
treehouse:~/workspace$                                                            
Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

Sorry. They seem to have rearranged things in newer workspaces since the last time I dug into an issue similar to this.

Are you getting the exact same error message as the OP? Can you post the error you see?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

In the newer Workspace versions, coverage is in a new location.

treehouse:~/workspace$ head -1 /usr/local/pyenv/versions/3.5.0/bin/coverage
#!/usr/local/pyenv/versions/3.5.0/bin/python3.5
treehouse:~/workspace$ coverage --version
Coverage.py, version 4.0.3.
Documentation at https://coverage.readthedocs.org

You may need to create a new Workspace to get the correct environment.

@Theo Lekkas: my .local directory wasn't empty.

I followed the instructions above, first I issued the following command: pip install --upgrade coverage

After that I edited the coverage file like Chris suggested but then I got the following error when running the command: coverage run tests.py

Traceback (most recent call last):
File "/usr/local/pyenv/versions/3.5.0/lib/python3.5/site-packages/pkg_resources/init.py", line 651, in build_ma ster
ws.require(
requires)
File "/usr/local/pyenv/versions/3.5.0/lib/python3.5/site-packages/pkg_resources/
init.py", line 952, in require
needed = self.resolve(parse_requirements(requirements))
File "/usr/local/pyenv/versions/3.5.0/lib/python3.5/site-packages/pkg_resources/
init_.py", line 844, in resolve
raise VersionConflict(dist, req).with_context(dependent_req)
pkg_resources.VersionConflict: (coverage 4.0.3 (/usr/local/pyenv/versions/3.5.0/lib/python3.5/site-packages), Requirem ent.parse('coverage==3.7.1'))

During handling of the above exception, another exception occurred:

File "/home/treehouse/.local/bin/coverage", line 5, in <module>
from pkg_resources import load_entry_point
File "/usr/local/pyenv/versions/3.5.0/lib/python3.5/site-packages/pkg_resources/init.py", line 3084, in <module> @call_aside
File "/usr/local/pyenv/versions/3.5.0/lib/python3.5/site-packages/pkg_resources/
init.py", line 3070, in _call_as ide
f(*args, **kwargs)
File "/usr/local/pyenv/versions/3.5.0/lib/python3.5/site-packages/pkg_resources/
init.py", line 3097, in _initial ize_master_working_set
working_set = WorkingSet._build_master()
File "/usr/local/pyenv/versions/3.5.0/lib/python3.5/site-packages/pkg_resources/
init.py", line 653, in _build_ma ster
return cls._build_from_requirements(
requires)
File "/usr/local/pyenv/versions/3.5.0/lib/python3.5/site-packages/pkg_resources/
init.py", line 666, in _build_fr om_requirements
dists = ws.resolve(reqs, Environment())
File "/usr/local/pyenv/versions/3.5.0/lib/python3.5/site-packages/pkg_resources/
init_.py", line 839, in resolve
raise DistributionNotFound(req, requirers)
pkg_resources.DistributionNotFound: The 'coverage==3.7.1' distribution was not found and is required by the application

I replaced the coverage version from 3.7.1 to 4.0.3 in the coverage file ran command again and it was fixed. Didn't see any mention of this last part so thought I'd share it.

Theo Lekkas
Theo Lekkas
5,908 Points

Chris Freeman , yes it's the same error:

treehouse:~/workspace/dice$ coverage run tests.py                                                
bash: /home/treehouse/.local/bin/coverage: /usr/local/pyenv/versions/3.4.1/bin/python3.5: bad interpreter: No such file or directory
Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

Hi Theo, It looks like the Workspaces using .local/bin are out of date. Please try to create a new Workspace. The version of coverage and it's referenced Python should be:

treehouse:~/workspace$ head -1 /usr/local/pyenv/versions/3.5.0/bin/coverage
#!/usr/local/pyenv/versions/3.5.0/bin/python3.5
treehouse:~/workspace$ coverage --version
Coverage.py, version 4.0.3.
Documentation at https://coverage.readthedocs.org
Ellen Shelton
Ellen Shelton
9,049 Points

Chris Freeman, I'm getting the exact same error as OP and Theo.

ETA: this is what I get when I run the commands you suggested--

treehouse:/~workspace$ head -1 ~/.local/bin/coverage
#!/usr/local/pyenv/versions/3.4.1/bin/python3.4
treehouse:/~workspace$ python --version
Python 3.5.0

When I try to update coverage, it says it's already up-to-date:

coverage in /usr/local/pyenv/versions/3.5.0/lib/python3.5/site-packages
Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

Hi Ellen, The Workspace environment seems to have changed relative to the versions of installed programs. Please try to create a new Workspace. coverage should be:

treehouse:~/workspace$ head -1 /usr/local/pyenv/versions/3.5.0/bin/coverage
#!/usr/local/pyenv/versions/3.5.0/bin/python3.5
treehouse:~/workspace$ coverage --version
Coverage.py, version 4.0.3.
Documentation at https://coverage.readthedocs.org