Matt Norris
Matt Norris

Reputation: 8836

"No source for code" message in Coverage.py

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.

Upvotes: 62

Views: 31954

Answers (12)

se7en
se7en

Reputation: 870

I had this problem, and what solved it was removing the .pytest_cache folder and the *.pyc files

Upvotes: 0

Fábio Costa
Fábio Costa

Reputation: 93

This question was made 13 years ago but today I faced this issue as well and to me, what worked was to delete the .coverage file and run coverage again.

Although the accepted answer works, I don't recommend to do it since it might hide some other issues you might have.

I suspect that for some the reference to a renamed file was left in the index.

Upvotes: 1

Давид Шико
Давид Шико

Reputation: 417

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

Upvotes: 0

Mikaelblomkvistsson
Mikaelblomkvistsson

Reputation: 3264

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.

Upvotes: 0

Xopi García
Xopi García

Reputation: 386

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

Upvotes: 1

Danny D'Amours
Danny D'Amours

Reputation: 712

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.

Upvotes: 1

Rick Hanlon II
Rick Hanlon II

Reputation: 21667

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 {} \;

Upvotes: 9

Adam Spence
Adam Spence

Reputation: 3240

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

Upvotes: 10

Jorge Vargas
Jorge Vargas

Reputation: 6932

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.

Upvotes: 0

xyzzyrz
xyzzyrz

Reputation: 16416

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.

Upvotes: 19

Ross
Ross

Reputation: 18111

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

Upvotes: 53

Ned Batchelder
Ned Batchelder

Reputation: 375574

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).

Upvotes: 61

Related Questions