【问题标题】:Pytest "Error: could not load path/to/conftest.py"Pytest“错误:无法加载路径/到/conftest.py”
【发布时间】:2018-02-08 22:45:47
【问题描述】:

当我尝试运行pytest repo/tests/test_file.py 时出现以下错误:

$ pytest repo/tests/test_file.py
Traceback (most recent call last):
  File "/Users/marlo/anaconda3/envs/venv/lib/python3.6/site-packages/_pytest/config.py", line 329, in _getconftestmodules
    return self._path2confmods[path]
KeyError: local('/Users/marlo/repo/tests/test_file.py')

During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/Users/marlo/anaconda3/envs/venv/lib/python3.6/site-packages/_pytest/config.py", line 329, in _getconftestmodules
    return self._path2confmods[path]
KeyError: local('/Users/marlo/repo/tests')

During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/Users/marlo/anaconda3/envs/venv/lib/python3.6/site-packages/_pytest/config.py", line 362, in _importconftest
    return self._conftestpath2mod[conftestpath]
KeyError: local('/Users/marlo/repo/conftest.py')

During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/Users/marlo/anaconda3/envs/venv/lib/python3.6/site-packages/_pytest/config.py", line 368, in _importconftest
    mod = conftestpath.pyimport()
  File "/Users/marlo/anaconda3/envs/venv/lib/python3.6/site-packages/py/_path/local.py", line 686, in pyimport
    raise self.ImportMismatchError(modname, modfile, self)
py._path.local.LocalPath.ImportMismatchError: ('conftest', '/home/venvuser/venv/conftest.py', local('/Users/marlo/repo/conftest.py'))
ERROR: could not load /Users/marlo/repo/conftest.py

我的回购结构是

lib/
    -tests/
        -test_file.py
app/
    -test_settings.py
pytest.ini
conftest.py
...

其他人已经很好地运行了这段代码,并且根据this question(和this one),我的结构很好,我没有丢失任何文件。我只能得出结论,我的计算机或项目设置不正确。如果您有任何我可能遗漏的建议或见解,请发送给我!

-------------------更多详情-------------- ----------------

test_file.py

def func(x):
    return x + 1

def test_answer():
    assert func(3) == 5

pytest.ini

[pytest]
DJANGO_SETTINGS_MODULE = app.test_settings
python_files = tests.py test_* *_tests.py *test.py

【问题讨论】:

  • 1.假设包含lib/app/conftest.py 的根目录名为repo - 文件repo/tests/test_file.py 是否存在? 2.运行cd repo; pytest --collect-only,所有的测试都找到了吗? 3、/home/venvuser/venv/conftest.py是什么文件,为什么要加载?它甚至属于另一个用户而不是你,这是故意的吗?检查您的sys.path 是否包含一些不需要的条目。
  • @hoefling 感谢您的建议!事实证明,我有一个 docker 容器,它具有不同的文件路径,它干扰了 pytest。
  • 我在app/tests 中添加了一个空的__init__.py,它起作用了。

标签: pytest


【解决方案1】:

我有 docker 以及在 docker 之外运行 pytest,对我来说,每当出现这种情况时,一个影响较小的修复方法是删除所有已编译的 python 文件

find . -name \*.pyc -delete

【讨论】:

  • 我很确定我也试过这个,但这对我的情况来说还不够。但是如果有更多人支持它,我会接受它作为答案:)
  • 对我的情况来说已经足够了(但我用git clean -dfqx 做到了)。
  • @JulienPalard 您应该警告人们不要使用该命令。它会删除更多的 .pyc 文件
  • 我不确定它是如何删除超过 .pyc 文件的?但是 'git clean -dfqx' 命令绝对看起来更干净。我现在无法对此进行测试(这些天不使用 docker),但如果其他人确认我会清理这个答案。
【解决方案2】:

我想通了,如果其他人有同样的问题,我会回答:

我什至没有考虑到我在 repo 目录中有一个 docker 容器(同一个应用程序),虽然我没有运行 docker 容器,但它以某种方式影响了文件路径。

解决这个问题:

  1. 我将远程源中的 repo 重新克隆到一个新文件夹中,这样旧 repo 中的任何内容都不会“污染”它。
  2. 使用 clean repo 的 .yml 规范更新了我的虚拟环境
$ conda env update --name 项目 --file project.yml
  1. 我的项目使用了一个 postgres 数据库,所以我删除了它并创建了一个新的
$ dropdb 项目数据库 $ createdb 项目数据库
  1. 由于我的项目使用 mongo,我也删除了该数据库
$ mongo projectdb --eval "db.dropDatabase()"
  1. 安装了一个干净的pytest
$ pip 卸载 pytest $点安装pytest

...瞧!我可以运行 pytest。

非常感谢@hoefling 和其他帮助我调试的人。

【讨论】:

    【解决方案3】:

    我也在运行 docker,但似乎我的问题有所不同。 我使用的是旧版本的 pytest:

    platform linux -- Python 3.9.7, pytest-3.7.2, py-1.10.0, pluggy-1.0.0
    

    在我的 ubuntu 映像默认拉出 python 3.10 后停止工作。 我的解决方案是更新(并修复)要使用的 dockerfile 映像:

    FROM python:3.10
    

    代替python:latest,同时更新pytest版本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-05
      • 1970-01-01
      • 1970-01-01
      • 2017-04-12
      • 1970-01-01
      • 1970-01-01
      • 2015-08-15
      • 1970-01-01
      相关资源
      最近更新 更多