简体   繁体   English

如何在 virtualenv 中使用 pytest?

[英]How do I use pytest with virtualenv?

I installed pytest into a virtual environment (using virtualenv ) and am running it from that virtual environment, but it is not using the packages that I installed in that virtual environment.我将pytest安装到虚拟环境中(使用virtualenv )并从该虚拟环境中运行它,但它没有使用我在该虚拟环境中安装的包。 Instead, it is using the main system packages.相反,它使用主系统包。 (Using python -m unittest discover , I can actually run my tests with the right python and packages, but I want to use the py.test framework.) (使用python -m unittest discover ,我实际上可以使用正确的 python 和包运行我的测试,但我想使用 py.test 框架。)

Is it possible that py.test is actually not running the pytest inside the virtual environment and I have to specify which pytest to run? py.test 是否有可能实际上没有在虚拟环境中运行 pytest 而我必须指定要运行哪个 pytest?

How to I get py.test to use only the python and packages that are in my virtualenv?如何让 py.test 仅使用我的 virtualenv 中的 python 和包?

Also, since I have several version of Python on my system, how do I tell which Python that Pytest is using?另外,由于我的系统上有多个 Python 版本,我如何知道 Pytest 使用的是哪个 Python? Will it automatically use the Python within my virtual environment, or do I have to specify somehow?它会在我的虚拟环境中自动使用 Python,还是我必须以某种方式指定?

在您的环境中,您可以尝试

python -m pytest

There is a bit of a dance to get this to work:有一些舞蹈可以让它发挥作用:

  1. activate your venv : source venv/bin/activate激活你的 venv : source venv/bin/activate
  2. install pytest : pip install pytest安装 pytest : pip install pytest
  3. re-activate your venv: deactivate && source venv/bin/activate重新激活你的 venv: deactivate && source venv/bin/activate

The reason is that the path to pytest is set by the source ing the activate file only after pytest is actually installed in the venv .原因是pytest的路径是在pytest实际安装在venv之后pytestactivate文件的source设置的。 You can't set the path to something before it is installed.在安装之前,您无法设置路径。

Re- activate ing is required for any console entry points installed within your virtual environment.安装在虚拟环境中的任何控制台入口点都需要重新activate

In my case I was obliged to leave the venv (deactivate), remove pytest (pip uninstall pytest), enter the venv (source /my/path/to/venv), and then reinstall pytest (pip install pytest).就我而言,我不得不离开 venv(停用),删除 pytest(pip uninstall pytest),输入 venv(source /my/path/to/venv),然后重新安装 pytest(pip install pytest)。 I don't known exacttly why pip refuse to install pytest in venv (it says it already present).我不知道为什么 pip 拒绝在 venv 中安装 pytest(它说它已经存在)。

I hope this helps我希望这有帮助

you have to activate your python env every time you want to run your python script, you have several ways to activate it, we assume that your virtualenv is installed under /home/venv :每次要运行 python 脚本时,都必须激活 python env,有几种方法可以激活它,我们假设您的 virtualenv 安装在 /home/venv 下:

1- the based one is to run the python with one command line >>> /home/venv/bin/python <your python file.py> 1- 基础是用一个命令行运行python >>> /home/venv/bin/python <your python file.py>

2- add this line on the top of python script file #! /home/venv/bin/python 2- 在 python 脚本文件的顶部添加这一行#! /home/venv/bin/python #! /home/venv/bin/python and then run python <you python file.py> #! /home/venv/bin/python然后运行python <you python file.py>

3- activate your python env source /home/venv/bin/activate and then run you script like python <you python file.py> 3- 激活你的 python env source /home/venv/bin/activate然后像python <you python file.py>一样运行你的脚本

4- use virtualenvwrapper to manager and activate your python environments 4- 使用virtualenvwrapper来管理和激活你的 python 环境

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM