Pytest 在使用 tox 时说“ModuleNotFoundError”

Max*_*wer 9 python pytest tox

我有以下项目结构:

root
|- module
  |- module.py
  |- __init__.py
|- tests
   |- unit
      |- some_test.py
   |- integration
      |- another_test.py
|- conftest.py
|- setup.py
|- tox.ini
Run Code Online (Sandbox Code Playgroud)

当我运行时,python3 module/module.py ...它按预期运行。

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

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

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

[testenv]
deps = pytest
commands = pytest
Run Code Online (Sandbox Code Playgroud)

有没有人知道为什么运行tox会让我找不到模块“dateutil”以及如何修复它?

phd*_*phd 10

[tox]skipsdist = True 阻止 tox运行,python setup.py sdist因此您install_requires的完全被忽略。

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

[testenv]
deps =
    -rrequirements.txt
Run Code Online (Sandbox Code Playgroud)

tox.ini。或者直接

[testenv]
deps = python-dateutil
Run Code Online (Sandbox Code Playgroud)


Fed*_*890 5

什么帮助了我:

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

  • 我正在使用 flit,但删除 `.tox` 目录并重新运行 `tox` 解决了我的问题。谢谢! (2认同)