我发现问题的最接近的问题是ImportError shows up with py.test but no when running the app,除了在我的情况下,我从我在虚拟环境中使用pip安装的外部库中获取了ImportError。
我得到的错误是:
==================================== 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'
奇怪的是,我可以运行scraper.py就好了没有错误。另外,明确安装了bs4,如我所说的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)
我尝试过以前的答案建议的解决方案:将PYTHONPATH设置为项目目录并在所述目录中创建conftest.py文件。
$ pwd
/Users/kgero/Documents/Projects/PoetryEngine
$ echo $PYTHONPATH
/Users/kgero/Documents/Projects/PoetryEngine
$ ls
__pycache__ features poems venv
conftest.py main.py scraper
那为什么pytest不能成功找到模块?