我将 pytest
安装到虚拟环境中(使用 virtualenv
)并从该虚拟环境运行它,但它没有使用我在该虚拟环境中安装的软件包。相反,它使用主系统包。 (使用 python -m unittest discover
,我实际上可以使用正确的 python 和包运行我的测试,但我想使用 py.test 框架。)
py.test 是否有可能实际上没有在虚拟环境中运行 pytest 而我必须指定要运行哪个 pytest?
如何让 py.test 只使用我的 virtualenv 中的 python 和包?
另外,由于我的系统上有多个版本的 Python,我如何知道 Pytest 使用的是哪个 Python?它会在我的虚拟环境中自动使用 Python,还是我必须以某种方式指定?
原文由 Henry Grantham 发布,翻译遵循 CC BY-SA 4.0 许可协议
有一些舞蹈可以让它发挥作用:
source venv/bin/activate
pip install pytest
deactivate && source venv/bin/activate
The reason is that the path to
pytest
is set by thesource
ing theactivate
file only afterpytest
is actually installed in thevenv
。您不能在安装之前设置路径。Re-
activate
是虚拟环境中安装的任何控制台入口点所必需的。