Python py.test无法从conftest正确导入模块-ConftestImportFailure失败:ModuleNotFound

Python py.test无法从conftest正确导入模块-ConftestImportFailure失败:ModuleNotFound,python,pytest,Python,Pytest,pytest无法导入模块,即使这些模块在vanillapython解释器下可以很好地导入。尤其是从conftest导入的模块无法解析。当pytest试图重写模块中的断言信息时,会引发ImportErrori 回溯示例: ============================================================================================================================================

pytest
无法导入模块,即使这些模块在vanilla
python
解释器下可以很好地导入。尤其是从
conftest
导入的模块无法解析。当pytest试图重写模块中的断言信息时,会引发ImportErrori

回溯示例:

================================================================================================================================================ test session starts ================================================================================================================================================
platform darwin -- Python 3.6.2, pytest-3.8.1, py-1.5.4, pluggy-0.7.1 -- /yyy/venv/bin/python3.6
cachedir: .pytest_cache
rootdir: /yyy/corporategovernance, inifile: setup.cfg
plugins: timeout-1.3.1, cov-2.5.1, populus-1.9.0, flaky-3.4.0, celery-4.1.0, splinter-1.8.5+headless
collecting 0 items / 1 errors                                                                                                                                                                                                                                                                                       
====================================================================================================================================================== ERRORS =======================================================================================================================================================
_________________________________________________________________________________________________________________________________ ERROR collecting corporategovernance/__init__.py __________________________________________________________________________________________________________________________________
../venv/lib/python3.6/site-packages/_pytest/config/__init__.py:381: in _getconftestmodules
    return self._path2confmods[path]
E   KeyError: local('/xxx/tests')

During handling of the above exception, another exception occurred:
../venv/lib/python3.6/site-packages/_pytest/config/__init__.py:412: in _importconftest
    return self._conftestpath2mod[conftestpath]
E   KeyError: local('/xxx/tests/conftest.py')

During handling of the above exception, another exception occurred:
../venv/lib/python3.6/site-packages/_pytest/config/__init__.py:418: in _importconftest
    mod = conftestpath.pyimport()
../venv/lib/python3.6/site-packages/py/_path/local.py:668: in pyimport
    __import__(modname)
../venv/lib/python3.6/site-packages/_pytest/assertion/rewrite.py:290: in load_module
    six.exec_(co, mod.__dict__)
corporategovernance/tests/conftest.py:4: in <module>
    import corporategovernance.backend
E   ModuleNotFoundError: No module named 'corporategovernance.backend'

During handling of the above exception, another exception occurred:
../venv/lib/python3.6/site-packages/_pytest/runner.py:201: in __init__
    self.result = func()
../venv/lib/python3.6/site-packages/_pytest/runner.py:261: in <lambda>
    call = CallInfo(lambda: list(collector.collect()), "collect")
../venv/lib/python3.6/site-packages/_pytest/python.py:624: in collect
    for x in self._collectfile(path):
../venv/lib/python3.6/site-packages/_pytest/python.py:579: in _collectfile
    ihook = self.gethookproxy(path)
../venv/lib/python3.6/site-packages/_pytest/python.py:568: in gethookproxy
    my_conftestmodules = pm._getconftestmodules(fspath)
../venv/lib/python3.6/site-packages/_pytest/config/__init__.py:395: in _getconftestmodules
    mod = self._importconftest(conftestpath)
../venv/lib/python3.6/site-packages/_pytest/config/__init__.py:431: in _importconftest
    raise ConftestImportFailure(conftestpath, sys.exc_info())
E   _pytest.config.ConftestImportFailure: ModuleNotFoundError("No module named 'corporategovernance.backend'",)
E     File "/yyy/venv/lib/python3.6/site-packages/_pytest/assertion/rewrite.py", line 290, in load_module
E       six.exec_(co, mod.__dict__)
E     File "/xxx/tests/conftest.py", line 4, in <module>
E       import corporategovernance.backend
===Flaky Test Report===
=====================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================
平台darwin——Python 3.6.2、pytest-3.8.1、py-1.5.4、pluggy-0.7.1——/yyy/venv/bin/python3.6
cachedir:.pytest\u缓存
rootdir:/yyy/corporategovernance,ini文件:setup.cfg
插件:timeout-1.3.1、cov-2.5.1、populus-1.9.0、flaky-3.4.0、芹菜-4.1.0、Splitter-1.8.5+无头
正在收集0项/1个错误
============================================================================================================================================================错误=======================================================================================================================================================
_________________________________________________________________________________________________________________________________收集corporategovernance/\uuuu init\uuuuu.py时出错__________________________________________________________________________________________________________________________________
../venv/lib/python3.6/site-packages/\u-pytest/config/\u\u-init\u\u.py:381:in\u-getconftestmodules
返回自我。\u路径2confmods[path]
E键错误:本地(“/xxx/tests”)
在处理上述异常期间,发生了另一个异常:
../venv/lib/python3.6/site-packages/\u-pytest/config/\u\u-init\u\u.py:412:in\u-importconftest
返回self.\u conftestpath2mod[conftestpath]
E键错误:本地('/xxx/tests/conftest.py'))
在处理上述异常期间,发生了另一个异常:
../venv/lib/python3.6/site-packages/\u-pytest/config/\u\u-init\u\u.py:418:in\u-importconftest
mod=conftestpath.pyimport()
../venv/lib/python3.6/site-packages/py/_-path/local.py:668:in-pyimport
__导入(modname)
../venv/lib/python3.6/site-packages/\u-pytest/assertion/rewrite.py:290:in-load\u模块
六、执行官(公司、国防部)
公司治理/tests/conftest.py:4:in
导入corporategovernance.backend
E ModuleNotFoundError:没有名为“corporategovernance.backend”的模块
在处理上述异常期间,发生了另一个异常:
../venv/lib/python3.6/site-packages/\u-pytest/runner.py:201:in\uu-init__
self.result=func()
../venv/lib/python3.6/site-packages/_-pytest/runner.py:261:in
call=CallInfo(lambda:list(collector.collect()),“collect”)
../venv/lib/python3.6/site-packages/_-pytest/python.py:624:in-collect
对于self.\u collectfile(路径)中的x:
../venv/lib/python3.6/site-packages/\u-pytest/python.py:579:in\u-collectfile
ihook=self.gethookproxy(路径)
../venv/lib/python3.6/site-packages/_-pytest/python.py:568:in-gethookproxy
my_conftestmodules=pm.\u getconftestmodules(fspath)
../venv/lib/python3.6/site-packages/\u-pytest/config/\u\u-init\u\u.py:395:in\u-getconftestmodules
mod=self.\u importconftest(conftestpath)
../venv/lib/python3.6/site-packages/\u-pytest/config/\u\u-init\u\u.py:431:in\u-importconftest
引发ConftestImportFailure(conftestpath,sys.exc_info())
E _pytest.config.ConftestImportFailure:ModuleNotFoundError(“没有名为'corporategovernance.backend'”的模块,)
E文件“/yyy/venv/lib/python3.6/site packages/\u pytest/assertion/rewrite.py”,第290行,在加载模块中
E六、执行官(公司、国防部)
E文件“/xxx/tests/conftest.py”,第4行,在
E导入corporategovernance.backend
==片状试验报告===

如何解决?

pytest如果在
tests
文件夹和子文件夹下有
\uu init\uuuuuy.py
文件,可能会很挑剔。这可能会导致pytest在某些情况下以错误的顺序解析Python模块


解决方法是删除测试中的所有
\uuuuu init\uuuuuuuuy.py
文件。

我实际上在
app/tests/
中添加了一个空的
\uuuuuu init\uuuuuuuuy.py
,效果很好<代码>平台linux--Python 3.7.3、pytest-4.4.0、py-1.8.0、Plugy-0.9.0--/home/roy/trainer/venv/bin/python3.7