我将 pytest
安装到虚拟环境中(使用 virtualenv
)并在该虚拟环境中运行它,但它没有使用我在该虚拟环境中安装的包环境。相反,它使用的是主系统包。 (使用 python -m unittest discover
,我实际上可以使用正确的 python 和包运行我的测试,但我想使用 py.test 框架。)
是否有可能 py.test 实际上并没有在虚拟环境中运行 pytest,我必须指定要运行哪个 pytest?
如何让 py.test 仅使用我的 virtualenv 中的 python 和包?
另外,由于我的系统上有多个版本的 Python,我如何判断 Pytest 使用的是哪个 Python?它会在我的虚拟环境中自动使用 Python,还是我必须以某种方式指定?
最佳答案
要让它发挥作用,需要花点功夫:
- 激活你的venv:
source venv/bin/activate
- 安装 pytest:
pip install pytest
- 重新激活你的 venv:
deactivate && source venv/bin/activate
原因是 pytest
的路径是由 source
ing activate
文件只在 pytest
之后设置的> 实际上是安装在 venv
中的。你不能在安装之前设置路径。
虚拟环境中安装的任何控制台入口点都需要重新激活
。
关于python - 如何将 pytest 与 virtualenv 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35045038/