1

The closest thing I have found to my problem is this question, ImportError shows up with py.test but no when running the app, except in my case I am getting an ImportError from an external library I have installed with pip in my virtual environment.

The error I get is:

==================================== ERRORS ====================================
________________ ERROR collecting scraper/tests/test_scraper.py ________________
scraper/tests/test_scraper.py:1: in <module>
    from scraper import scraper
scraper/scraper.py:3: in <module>
    from bs4 import BeautifulSoup
E   ImportError: No module named 'bs4'

What is strange is that I can run scraper.py just fine with no errors. Plus, bs4 is clearly installed, as demonstrated with I call pip list:

$pip list
beautifulsoup4 (4.4.1)
bs4 (0.0.1)
pip (8.1.2)
py (1.4.31)
pytest (2.9.2)
setuptools (23.0.0)
wheel (0.29.0)

I have tried the solutions suggested from previous answers: setting the PYTHONPATH to the directory of the project and creating a conftest.py file in said directory.

$ pwd
/Users/kgero/Documents/Projects/PoetryEngine
$ echo $PYTHONPATH
/Users/kgero/Documents/Projects/PoetryEngine
$ ls
__pycache__ features    poems       venv
conftest.py main.py     scraper

So why can't pytest successfully find the module?

Community
  • 1
  • 1
oregano
  • 816
  • 9
  • 25
  • `py.test` is possibly run with an other Python version or environment. – Klaus D. Jun 25 '16 at 14:42
  • 1
    How are you executing `py.test`? Is it running in the same `virtualenv` that `bs4` is installed in? Are there any other external dependencies that it *does* find, or is `bs4` the first one that it tries to find? – jonrsharpe Jun 25 '16 at 14:42
  • I just run `$py.test` in the project directory. It is run in the same `virtualenv` as `bs4`. `bs4` is the first external dependency it tries to find. – oregano Jun 25 '16 at 14:58
  • If you comment that one out, does it get stuck on the second one? Does `python -c "import bs4"` also throw an `ImportError`? – jonrsharpe Jun 25 '16 at 15:05
  • `python -c "import bs4"` does not throw an error. If I comment out the import in the file, it gets stuck on the second one. – oregano Jun 25 '16 at 15:08
  • 2
    Interesting. How about `python -m pytest`, explicitly using the current interpreter? Or adding a `conftest.py`, as an answer on the question you've linked suggests? – jonrsharpe Jun 25 '16 at 15:09
  • 1
    `python -m pytest` did it! – oregano Jun 25 '16 at 15:14
  • It sound like py.test is outside the virtualenv (as @jonrsharpe is suggesting). Possibly you have another version installed globally. Try running ``py.test --version`` or ``whereis py.test`` to check the full path of the ``py.test`` executable. You can also try ``FULL_PATH_TO_VIRTUALENV/bin/py.test test.py`` to make sure you are using the correct py.test. – Omar Kohl Jun 25 '16 at 15:24
  • It does look like it was accessing the global version: `$py.test --version This is pytest version 2.9.2, imported from /usr/local/lib/python3.5/site-packages/pytest.py`. Not sure how to make it automatically look for the `virtualenv` version first. `venv/bin/py.test` also works. – oregano Jun 25 '16 at 15:39
  • Try uninstalling the global version and *only* having it in your virtual environments. You can use the environment variable `PIP_REQUIRE_VIRTUALENV=true` to make `pip` complain if you try to install packages globally in the future. – jonrsharpe Jun 25 '16 at 19:41

0 Answers0