【发布时间】: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/tcx2gpx在PYTHONPATH中。
我也尝试过使用python -m pytest,因为正如here 所述,这样做应该会自动将当前目录包含在syspath 中。我得到了相同的结果,ModuleNotFoundError。
我还发现了this article,它描述了安装pytest 的系统与虚拟环境中的系统之间的冲突,但首先我无法在系统级别卸载它,因为它是我安装在系统,其次,当我尝试在 GitLab CI 中运行测试时遇到同样的错误,我认为没有安装任何其他版本的pytest(我必须在测试阶段明确安装它)。
真的很难理解为什么这不起作用任何建议或指针将不胜感激。
【问题讨论】:
-
它是否显示在
pip list中?
标签: python-3.x virtualenv pytest