【问题标题】:pytest module not found找不到pytest模块
【发布时间】:2020-04-29 10:04:45
【问题描述】:

在我的包裹上运行 pytest 时遇到问题。我有以下结构...

 tree -f
.
├── [   0 Jan  4 22:04]  ./__init__.py
├── [ 34K Jan 12 11:37]  ./LICENSE
├── [1.4K Jan  6 07:43]  ./README.md
├── [1.1K Jan 12 11:42]  ./setup.cfg
├── [  79 Jan  6 08:22]  ./setup.py
├── [4.0K Jan 12 11:49]  ./tcx2gpx
│   ├── [ 701 Jan  6 23:13]  ./tcx2gpx/__init__.py
│   ├── [4.0K Jan 12 12:13]  ./tcx2gpx/__pycache__
│   │   ├── [ 683 Jan 12 12:13]  ./tcx2gpx/__pycache__/__init__.cpython-36.pyc
│   │   └── [3.0K Jan  6 21:00]  ./tcx2gpx/__pycache__/tcx2gpx.cpython-36.pyc
│   └── [3.1K Jan 12 11:49]  ./tcx2gpx/tcx2gpx.py
├── [4.0K Jan  4 21:52]  ./tests
│   ├── [4.0K Jan  6 06:32]  ./tests/resources
│   │   ├── [288K Jan  6 21:07]  ./tests/resources/2019-10-20 12:51:21.0.gpx
│   │   └── [816K Jan  4 22:03]  ./tests/resources/2019-10-20 12:51:21.0.tcx
│   └── [4.0K Jan 12 12:24]  ./tests/tcx2gpx
│       ├── [ 386 Jan 12 12:17]  ./tests/tcx2gpx/conftest.py
│       ├── [   0 Jan 12 11:48]  ./tests/tcx2gpx/__init__.py
│       └── [1.3K Jan 12 12:24]  ./tests/tcx2gpx/test_tcx2gpx.py
└── [4.0K Jan  6 06:21]  ./tmp
    ├── [288K Jan  6 06:21]  ./tmp/2019-10-20 12:51:21.0.gpx
    └── [ 415 Jan  5 08:38]  ./tmp/test.py

而我的conftest.py 有以下...

"""
Fixtures for test_tcx2gpx
"""
from pathlib import Path
import pytest

from tcx2gpx.tcx2gpx import TCX2GPX

TCX_DIR = Path(__file__).resolve().parents[1]
TCX_FILE = TCX_DIR / 'resources' / '2019-10-20 12:51:21.0.tcx'
GPX_FILE = TCX_DIR / 'resources' / '2019-10-20 12:51:21.0.gpx'


@pytest.fixture
def tcx_file():
    """
    Fixture of TCX file
    """
    return TCX2GPX(TCX_FILE)

在从顶层运行pytest 时,我被告知...

____________________________________________________ ERROR collecting test session ____________________________________________________
/home/neil/.virtualenvs/default/lib/python3.6/site-packages/_pytest/config/__init__.py:458: in _importconftest
    return self._conftestpath2mod[key]
E   KeyError: PosixPath('/mnt/work/python/tcx2gpx/tests/tcx2gpx/conftest.py')

During handling of the above exception, another exception occurred:
/home/neil/.virtualenvs/default/lib/python3.6/site-packages/_pytest/config/__init__.py:464: in _importconftest
    mod = conftestpath.pyimport()
/home/neil/.virtualenvs/default/lib/python3.6/site-packages/py/_path/local.py:701: in pyimport
    __import__(modname)
/home/neil/.virtualenvs/default/lib/python3.6/site-packages/_pytest/assertion/rewrite.py:143: in exec_module
    exec(co, module.__dict__)
tests/tcx2gpx/conftest.py:7: in <module>
    from tcx2gpx.tcx2gpx import TCX2GPX
E   ModuleNotFoundError: No module named 'tcx2gpx.tcx2gpx'

阅读后我发现了一些关于此的线程(例如here),因为我正在 GitLab 上研究 CI,我决定尝试修改 PYTHONPATH(因为我可以将这些作为 script 的一部分在 GitLab CI 执行时执行)...

export PYTHONPATH="$PYTHONPATH:."
python -c "import sys;print(sys.path)"
['', '/mnt/work/python/tcx2gpx', '/home/neil/.virtualenvs/default/lib64/python
36.zip', '/home/neil/.virtualenvs/default/lib64/python3.6', '/home/neil/.virtualenvs/default/lib64/python3.6/lib
-dynload', '/usr/lib64/python3.6', '/usr/lib/python3.6', '/home/neil/.virtualenvs/default/lib/python3.6/site-pac
kages', '/mnt/work/python/python-tcxparser']
pytest

但我得到同样的错误,即使上述目录结构所在的/mnt/work/python/tcx2gpxPYTHONPATH中。

我也尝试过使用python -m pytest,因为正如here 所述,这样做应该会自动将当前目录包含在syspath 中。我得到了相同的结果,ModuleNotFoundError

我还发现了this article,它描述了安装pytest 的系统与虚拟环境中的系统之间的冲突,但首先我无法在系统级别卸载它,因为它是我安装在系统,其次,当我尝试在 GitLab CI 中运行测试时遇到同样的错误,我认为没有安装任何其他版本的pytest(我必须在测试阶段明确安装它)。

真的很难理解为什么这不起作用任何建议或指针将不胜感激。

【问题讨论】:

  • 它是否显示在pip list 中?

标签: python-3.x virtualenv pytest


【解决方案1】:

你不应该在你的项目顶部有一个__init__.py 文件——这会导致 pytest 假设父目录是你项目的根目录

删除为我修复它

(免责声明:我是 pytest 核心开发人员)

【讨论】:

  • 同样不正确的导入语句from tcx2gpx.tcx2gpx import TCX2GPX 需要更改。
  • @wim 来自看起来不错的文件结构,有 tcx2gpx/tcx2gpx.py(这有点令人困惑,因为 OP 使用 tree -f 而不是普通的 tree
  • 啊,你是对的。我的错。看起来像一个太多级别的命名空间而不是必要的。正在测试的 tcx2gpx 包更令人困惑。
  • 谢谢,我删除了顶级__init__.py,但这并没有什么不同。删除tests/tcx2gpx 目录中的__init__.py 并运行python -m "pytest" 解决了它。感谢您的指点。我仍然对__init__.py 的需求感到困惑,因为我认为它是hangover from Python <3.3*,但也许我没有正常工作的命名空间,需要阅读!
  • 恼人地从测试文件夹中删除 __init__.py 会在 linting 上引入 F0010,但我可以努力消除它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-08-17
  • 1970-01-01
  • 1970-01-01
  • 2022-12-15
  • 2021-04-15
  • 2018-02-21
  • 1970-01-01
相关资源
最近更新 更多