Question

I'm having trouble getting the "coverage" module for Python working correctly.

I have no permissions to modify or add to Python directory, so am unable to install easy_install to setup coverage. Worked around this by adding the coverage directory to my python path. Coverage works fine, inputting covereage --version to shell outputs the version. coverage help displays all the commands available. Everything seems fine. I can run coverage fine on small simple scripts.

However, when running a large script with a number of imports across 5 modules (a script that has been tested as working), when running the command with coverage run [normal commands], the script executes and fails, because some modules cannot be found.

Here's a breakdown -

Command line without coverage (working fine):

I invoke the script by running: ./script_name.py arg1 arg2 arg3

When I try running the script by inputting: python script_name.py arg1 arg2 arg3, the script fails, failing to import modules:

    Error message:
        ImportError: No module named cx_Oracle

Command line with coverage:

coverage run ./script_name.py arg1 arg2 arg3

    "No file to run: 'main.py'"

coverage run {[full_path_to_module_name}./script_name.py arg1 arg2 arg3

    Now we are getting somewhere. Coverage is clearly invoked and working, put I get that import error:

Error message:
    ImportError: No module named cx_Oracle
Was it helpful?

Solution

The good news is, running with "python" explicitly, and with coverage.py produce the same result. The bad news is, that result is an error message.

It seems like you have more than one Python installation. One is found by "./script_name.py", the other is found by "python ./script_name.py".

To diagnose the problem, add these lines to the top of script_name.py:

import sys
print sys.executable
print "\n".join(sys.path)

This will show you the Python executable being invoked, and the search path for modules. Running your script both ways will show you different results, and you should be able to figure out what is going on.

OTHER TIPS

I think I had a similar issue, and managed to solve it by running coverage like this:

python -m coverage run [normal commands]

Specifically in my case it was

python -m coverage run -m unittest discover

It definitely seems to be a case of coverage using a different python installation, as my module that was reported missing was only installed in my virtual env and not in my global env.

I will add my experience, as someone could fall into the same mistake.

I was running coverage inside a python virtual environment (venv) with coverage not installed. Executing coverage I was in fact calling the coverage installed globally.

Installing locally with pip3 install coverage solved it for me, now using python3 -m coverage run myfile.py.

I have just encountered a very similar issue where using either of the following worked for me locally:

coverage run

python -m coverage run

So locally whenever I ran make unit-test either of these worked fine, yet when I deployed my code to codebuild on AWS neither of these make targets worked.

I finally put this down to the fact that over time I had installed coverage locally, and into my local environment, but on starting to use pyenv I also installed this into my pyenv environment, so three places locally.

When running on the build agent I was in fact ONLY installing this into a fresh pyenv environment, and so I had to use the following syntax:

python -m pipenv run coverage run --source=src/app -m pytest -s --junitxml=result.xml src/test/unit/*.py

I guess the moral of my story is to keep your working environment clean, which I will endeavour to do from now on.

This might help somebody out in a similar situation, which is why I am posting.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top