A test running with nosetests fails with ImportError, but works with python command

浪尽此生 提交于 2019-12-14 02:35:12

问题


When run a test with python mycore/tests4extractor.py it works. If run the test with nosetests ./mycore/tests4extractor.py it fails with ImportError: No module named extractor. I am in the helpers folder.

The project structure is:

helpers/
    mycore/
        __init__.py
        extractor.py
        tests4extractor.py

Setting PYTHONPATH to the absolute path to helpers and/or helpers/mycore doesn't help.

Answer

tests4extractor.py:

    import mycore
    from extractor import extract

should be changed to:

    import mycore
    from mycore.extractor import extract

And python should be run with python -mmycore.tests4_strings


回答1:


Does tests4extractor.py contain import extractor?

Because mycore is a package, you need to use absolute imports:

from mycore import extractor

or relative imports:

from . import extractor


来源:https://stackoverflow.com/questions/9429202/a-test-running-with-nosetests-fails-with-importerror-but-works-with-python-comm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!