Pytest 在使用 tox 时说 'ModuleNotFoundError'

Pytest says 'ModuleNotFoundError' when using tox

我有以下项目结构:

root
|- module
  |- module.py
  |- __init__.py
|- tests
   |- unit
      |- some_test.py
   |- integration
      |- another_test.py
|- conftest.py
|- setup.py
|- tox.ini

当我 运行 python3 module/module.py ... 它 运行 如预期的那样。

但是,当我执行 tox 时,我得到 ModuleNotFoundError: No module named 'dateutil'

在我的 setup.py 中有 install_requires=['python-dateutil'] 和 tox.ini 有以下(简化)内容:

[tox]
envlist   = py{36, 37}
skipsdist = True

[testenv]
deps = pytest
commands = pytest

有人知道为什么 运行ning tox 告诉我找不到模块 'dateutil' 以及如何修复它吗?

[tox]skipsdist = True prevents tox 到 运行 python setup.py sdist 所以你的 install_requires 被完全忽略了。

如果您真的想按照建议为应用程序设置 [tox]skipsdist = True,还建议您遵循打包应用程序的所有其他最佳实践:使用 requirements.txt 和添加

[testenv]
deps =
    -rrequirements.txt

tox.ini。或者直接

[testenv]
deps = python-dateutil

对我有什么帮助:

  1. 将缺少的模块添加到 setup.py
  2. install_requires 部分
  3. 删除旧的.tox目录并重新运行tox