Coverage doesn't resolve import absolute references

Issue #486 on hold
Gianluca Rossi created an issue

When running coverage run <my_test.py> absolute reference imports are not resolved. For instance I have a project structured in this way:

$ pwd
/home/gianluca/git/cheap_flight_finder

$ tree
.
├── api_connection.py
├── get_flight_price.py
├── __init__.py
├── LICENSE.txt
├── main.py
├── __pycache__
│   ├── __init__.cpython-35.pyc
│   └── quote.cpython-35.pyc
├── quote.py
├── README.md
└── test
    ├── __init__.py
    ├── __pycache__
    │   ├── __init__.cpython-35.pyc
    │   ├── test_example.cpython-35.pyc
    │   └── test_quote.cpython-35.pyc
    ├── test_api_connection.py
    ├── test_data_processing.py
    └── test_quote.py

The imports in the test_quote.py file are:

from nose.tools import assert_equal, assert_raises, raises
from cheap_flight_finder.quote import Quote

If I launch the command python test_quote.py this run smoothly. However coverage run test_quote.py returns the following error message:

$ coverage run test/test_quote.py 
Traceback (most recent call last):
  File "test/test_quote.py", line 2, in <module>
    from cheap_flight_finder.quote import Quote
ImportError: No module named 'cheap_flight_finder'

Comments (5)

  1. Ned Batchelder repo owner

    Interesting. Is this project online? It would be really helpful to have an entire reproducible test case.

  2. Gianluca Rossi reporter

    You can find the project here. Apologies in advance for the quality of the code and the general status of the project. I'm hoping to make it more presentable by end of next week.

  3. Ned Batchelder repo owner

    I tried to reproduce what you saw, but every way I tried to run test_quote.py, I got the ImportError from Python itself:

    $ pwd
    /Users/ned/foo/bug486/cheap_flight_finder
    $ tree
    .
    ├── LICENSE.txt
    ├── README.md
    ├── TODO.md
    ├── __init__.py
    └── cheap_flight_finder
        ├── __init__.py
        ├── api_connection.py
        ├── quote.py
        └── test
            ├── __init__.py
            ├── test_api_connection.py
            └── test_quote.py
    
    2 directories, 10 files
    $ python --version
    Python 3.5.1
    $ python cheap_flight_finder/test/test_quote.py
    Traceback (most recent call last):
      File "cheap_flight_finder/test/test_quote.py", line 2, in <module>
        from cheap_flight_finder.quote import Quote, QuotesCollection
    ImportError: No module named 'cheap_flight_finder'
    $ cd cheap_flight_finder/
    $ python test/test_quote.py
    Traceback (most recent call last):
      File "test/test_quote.py", line 2, in <module>
        from cheap_flight_finder.quote import Quote, QuotesCollection
    ImportError: No module named 'cheap_flight_finder'
    $ cd test
    $ python test_quote.py
    Traceback (most recent call last):
      File "test_quote.py", line 2, in <module>
        from cheap_flight_finder.quote import Quote, QuotesCollection
    ImportError: No module named 'cheap_flight_finder'
    

    Is there some other thing I need to do?

  4. Log in to comment