Pytest "Error: could not load path/to/conftest.py"
Pytest "Error: could not load path/to/conftest.py"
当我尝试 运行 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 (and 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
我弄明白了,如果其他人有同样的问题,我会回答的:
我什至没有考虑到我在 repo 目录中有一个 docker 容器(属于同一个应用程序),虽然我没有 运行 宁 docker 容器,它以某种方式影响了文件路径。
解决这个问题:
- 我将 repo 从远程源重新克隆到一个新文件夹中,这样旧 repo 中的任何内容都无法 "contaminate" 它。
- 使用 clean repo 的 .yml 规范更新了我的虚拟环境
$ conda env update --name project --file project.yml
- 我的项目使用了 postgres 数据库,所以我放弃了它并创建了一个新的
$ dropdb projectdb
$ createdb projectdb
- 由于我的项目使用 mongo,我也删除了那个数据库
$ mongo projectdb --eval "db.dropDatabase()"
- 安装了一个干净的
pytest
$ pip uninstall pytest
$ pip install pytest
...瞧!我可以 运行 pytest.
非常感谢@hoefling 和其他帮助我调试的人。
我在 docker 之外也有 docker 和 运行 pytest,对我来说,每当出现这种情况时,一个影响较小的修复方法是删除所有已编译的 python 个文件
find . -name \*.pyc -delete
我也是 运行 docker,但我的问题似乎有所不同。
我使用的是旧版本的 pytest:
platform linux -- Python 3.9.7, pytest-3.7.2, py-1.10.0, pluggy-1.0.0
在我的 ubuntu 图像默认拉取 python 3.10 后停止工作。
我的解决方案是更新(并修复)要使用的 docker 文件图像:
FROM python:3.10
代替python:latest
,同时更新pytest版本。
当我尝试 运行 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 (and 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
我弄明白了,如果其他人有同样的问题,我会回答的:
我什至没有考虑到我在 repo 目录中有一个 docker 容器(属于同一个应用程序),虽然我没有 运行 宁 docker 容器,它以某种方式影响了文件路径。
解决这个问题:
- 我将 repo 从远程源重新克隆到一个新文件夹中,这样旧 repo 中的任何内容都无法 "contaminate" 它。
- 使用 clean repo 的 .yml 规范更新了我的虚拟环境
$ conda env update --name project --file project.yml
- 我的项目使用了 postgres 数据库,所以我放弃了它并创建了一个新的
$ dropdb projectdb $ createdb projectdb
- 由于我的项目使用 mongo,我也删除了那个数据库
$ mongo projectdb --eval "db.dropDatabase()"
- 安装了一个干净的
pytest
$ pip uninstall pytest $ pip install pytest
...瞧!我可以 运行 pytest.
非常感谢@hoefling 和其他帮助我调试的人。
我在 docker 之外也有 docker 和 运行 pytest,对我来说,每当出现这种情况时,一个影响较小的修复方法是删除所有已编译的 python 个文件
find . -name \*.pyc -delete
我也是 运行 docker,但我的问题似乎有所不同。 我使用的是旧版本的 pytest:
platform linux -- Python 3.9.7, pytest-3.7.2, py-1.10.0, pluggy-1.0.0
在我的 ubuntu 图像默认拉取 python 3.10 后停止工作。 我的解决方案是更新(并修复)要使用的 docker 文件图像:
FROM python:3.10
代替python:latest
,同时更新pytest版本。