"No source for code" message in Coverage.py

Question:

I ran a build last night, successfully. I got up this morning and ran another without changing any configuration or modifying any source code. Now my build is failing with the message “No source for code” when running my nosetests with coverage.

NoSource: No source for code: '/home/matthew/.hudson/jobs/myproject/workspace/tests/unit/util.py'
. . . 
No source for code: '/home/matthew/.hudson/jobs/myproject/workspace/__init__.py'

The only clue I have is that the files it says it can’t find aren’t there, but they never were and they’re not supposed to be. For example, in the latter, Hudson’s workspace isn’t a Python module, so __init__.py wouldn’t be there.

Update: I’ve confirmed that this isn’t a Hudson issue. When I run nostests with coverage in the directory itself, I see similar messages. Again, the files that coverage is looking for were never there to begin with, making this very puzzling.

Asked By: Matt Norris

||

Answers:

I’m not sure why it thinks that file exists, but you can tell coverage.py to ignore these problems with a coverage xml -i switch.

If you want to track down the error, drop me a line (ned at ned batchelder com).

Answered By: Ned Batchelder

Ensure theres no .pyc file there, that may have existed in the past.

Answered By: Ross

Summary: Existing .coverage data is kept around when running nosetests --with-coverage, so remove it first.

Details: I too just encountered this via Hudson and nosetests. This error was coming from coverage/results.py:18 (coverage 3.3.1 – there were 3 places raising this error, but this was the relevant one). It’s trying to open the .py file corresponding to the module that was actually traced. A small demo:

$ echo print > hello.py
$ echo import hello > main.py
$ coverage run main.py

$ rm hello.py
$ coverage xml
No source for code: '/tmp/aoeu/hello.py'

Apparently I had a file stopwords.pyc that was executed/traced, but no stopwords.py. Yet nowhere in my code was I importing stopwords, and even removing the .pyc I still got the error.

A simple strings .coverage then revealed that the reference to stopwords.py still existed. nosetests --with-coverage is using coverage’s append or merge functionality, meaning the old .coverage data still lingers around. Indeed, removing .coverage addressed the issue.

Answered By: Yang

Maybe this will help, but I ran into a similar error today. And it’s a permission error. My code is using a checkout from another user (by design, down ask) and I need to sudo in order for coverage to work. So your issue may have something to it.

Answered By: Jorge Vargas

Just use the ‘–cover-erase’ argument. It fixes this error and you don’t have to manually delete coverage files

nosetests --with-coverage --cover-erase

I’d strongly recommend checking out the help to see what other args you’re missing too and don’t forget those plugins either

Answered By: Adam Spence

The problem is that the .pyc file still exists.

A quick and dirty solution is to delete all .pyc files in that directory:

find . -name "*.pyc" -exec rm -rf {} ;
Answered By: Rick Hanlon II

I ran into this problem as well when trying to run nosetests coverage through setuptools. As mentioned, it is possible to delete existing .pyc files but that can be cumbersome.

I ended up having to create a .coveragerc file with the following

[report]

ignore_errors = True

to fix this error.

Answered By: Danny D'Amours

coverage report -m can be called just so, without providing any argument (see official quick instructions).
But it works on 1st script coverage-ed, not on 2nd. E.g.

coverage run -m f1.py
coverage report -m # works
coverage run -m f2.py
coverage report -m # fails (f2.py instead of f1.py in last coverage run)

Instead, always indicate script as argument of coverage report -m:

file="f2.py" && coverage run $file && coverage report -m $file

Coverage reporting docs

Answered By: Xopi García

I had this problem. pytest-cov claimed there is no code for existing files full of valid and covered code. I removed those warnings just by removing .coverage file. It is of course recreated on next runs.

Answered By: Mikaelblomkvistsson

A bit another case, but anyway…
Don’t be foolish as me, use just coverage html, not coverage report html

Answered By: Давид Шико