Tarik Kdiry
Tarik Kdiry

Reputation: 57

Coverage python library not found after installing

I ran the command pip install coverage and it appears to have installed correctly.

Frodo:Triangle567 tarikkdiry$ pip install coverage
    Collecting coverage
      Using cached https://files.pythonhosted.org/packages/c7/d0/337673c08f5b0cc7ada3dfe2a998ae8a97d482722045644be3d79bbcbe05/coverage-4.5.1-cp37-cp37m-macosx_10_13_x86_64.whl
    Installing collected packages: coverage
    Successfully installed coverage-4.5.1

However, after running coverage on one of my test files, I receive this error:

Frodo:Triangle567 tarikkdiry$ coverage run testtriangle.py
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/bin/coverage", line 7, in <module>
    from coverage.cmdline import main
ModuleNotFoundError: No module named 'coverage.cmdline'; 'coverage' is not a package
Frodo:Triangle567 tarikkdiry$

I have tried uninstalling every python package and reinstalling but to no success. I have tried this on another machine and can confirm the test file is working properly.

EDIT: After running pip3 check coverage and pip3 show coverage

pip3 check coverage:

No broken requirements found.

pip3 show coverage

Name: coverage
Version: 4.5.1
Summary: Code coverage measurement for Python
Home-page: https://bitbucket.org/ned/coveragepy
Author: Ned Batchelder and 100 others
Author-email: [email protected]
License: Apache 2.0
Location: /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages
Requires:
Required-by:

Upvotes: 3

Views: 30182

Answers (4)

Pieter
Pieter

Reputation: 1201

What worked for me:

python -m coverage run arg1 arg2 arg3

Instead of what is mentioned on https://coverage.readthedocs.io/en/6.0.2/

coverage run arg1 arg2 arg3

Which is similar to this answer for Windows: https://stackoverflow.com/a/36656924/264359

Upvotes: 4

Damon Stamper
Damon Stamper

Reputation: 428

I encountered this on Windows when running pip install coverage when using the mingw64 (bash for Windows) shell. Pip detected a linux environment and didn't include the .exe that Windows needs.

To resolve this I ran pip uninstall coverage then opened an administrative PowerShell prompt and ran pip install coverage.

Upvotes: 2

jwodder
jwodder

Reputation: 57610

According to the comments, you have a file named coverage.py in your current working directory. This file is interfering with the coverage command's attempts to import from coverage.cmdline. You need to rename the file to something else.

Upvotes: 1

Eir Nym
Eir Nym

Reputation: 1599

You should check if coverage refer to python or python3. Most likely it uses python to run exact tool you need. Default python version on macOS is python 2.7.

Another option is to create a virtual environment by running python3.7 -m venv $directory (where $directory contains a folder for virtual environment, you can use direct paths as you wish), then activating it by running source $directory/bin/activate. After doing this, you'll have no problems finding all packages you want. I personally prefer this method over installing packages into the system.

Additionally, you can install python3.7 from MacPorts or Homebrew and activate it as a default python. For many libraries you can install them using these package managers as well.

Upvotes: 4

Related Questions