Python 3.x 找不到pytest模块

Python 3.x 找不到pytest模块,python-3.x,virtualenv,pytest,Python 3.x,Virtualenv,Pytest,在我的包上运行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] ./tc

在我的包上运行
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'
但是我得到了相同的错误,即使上述目录结构所在的
/mnt/work/python/tcx2gpx
位于
PYTHONPATH

我还尝试过使用
python-mpytest
,因为如上所述,这样做应该自动将当前目录包含在
syspath
中。但是我得到了相同的结果,
ModuleNotFoundError

我还发现它描述了安装的
pytest
系统与虚拟环境中的系统之间的冲突,但首先我无法在系统级别卸载它,因为它是我在系统中安装的其他软件包的必需依赖项,其次,我在GitLab CI中运行测试时遇到了相同的错误,我认为没有安装任何其他版本的
pytest
(我必须在测试阶段显式安装它)


如果您真的想知道为什么这样做不起作用,我们将非常感谢您的任何建议或指针。

您不应该在项目顶部有一个
\uuuu init\uuuuuuuuupy.py
文件——这会导致pytest假定父目录是项目的根目录

删除它为我修复了它


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

它是否显示在
pip列表中
?tcx2gpx.tcx2gpx导入tcx2gpx的导入语句
也不正确。需要更改tcx2gpx导入tcx2gpx
。@wim从看起来不错的文件结构来看,有
tcx2gpx/tcx2gpx.py
(这有点令人困惑,因为OP使用了
tree-f
而不是普通的
tree
)啊,你说得对。我的不好。似乎名称空间的级别太多了。而且测试中的
tcx2gpx
包更让人困惑。谢谢,我删除了顶层
\uuu init\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu目录并运行
python-m“pytest”
解决了这个问题。感谢指针。我仍然不清楚是否需要
\uuuu init\uuuuuuuuuuuy.py
,因为我认为这是python的宿醉,从test文件夹中删除
\uuuuu init\uuuuuuuuuuuuuuuuuuuuuuuuuy.py
会在linting上引入
f010,但我可以努力消除它。
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