问题描述
我已经安装了pyenv
,并安装了不同版本的python:
I've installed pyenv
and have different versions of python installed with it:
$ pyenv versions
system
2.7.1
3.2.5
3.5.0
3.5.1
* 3.5.2
我使用以下命令切换到python 3.5.2
:
I use the following command to switch to python 3.5.2
:
当我检查python版本时,这就是我得到的:
And when I check the python version this is what I get:
$ python --version
Python 3.5.2
但是当我运行pytest
时,它仍然在python 2.7.6
下运行:
But when I run pytest
, it still runs under python 2.7.6
:
为什么pytest
在旧版本下运行?
Why is pytest
running under the older version?
推荐答案
底线:运行
-
python -m pytest
或 -
py.test-<version>
(如果您的替代Python和pytest
已与系统软件包管理器一起安装),或者 - 如果您的替代Python已安装
pyenv
,请使用pyenv
切换到该版本 ,并确保已为其安装pytest
.然后,您可以运行pytest
.- 由于
pip
可执行文件也是要切换的可执行文件之一,因此,在安装pytest
之前,还需要切换到其他Python .
python -m pytest
, orpy.test-<version>
if your alternative Python andpytest
are installed with system package manager, or- if your alternative Python has been installed with
pyenv
, switch withpyenv
to that version and make sure you havepytest
installed for it. Then you can just runpytest
.- since the
pip
executable is also among the ones being switched, you need to switch to the alternative Python before installingpytest
for it, too.
如我所见,
/usr/bin/pytest
(属于系统软件包管理器的python-pytest
软件包)具有shebang!#/usr/bin/python
,因为它对应于系统python的安装.As I can see,
/usr/bin/pytest
(that belongs to the system package manager'spython-pytest
package) has a shebang!#/usr/bin/python
since it corresponds to the system python's installation.pyenv
,如其README.md
所述,它不能代替/usr/bin/python
- 因为它确实不应替换,以免破坏系统软件包..相反,它将目录添加到
PATH
并在其中插入启动器(称为" shim ),这是在您键入"python
"时调用的内容.您可能已经猜到了,这种骇客会被上述的Shebang忽略-应当如此.Instead, it adds its directory to
PATH
and inserts a launcher there (called "shim") which is what gets invoked when you type "python
". As you probably guessed, this hack is ignored by a shebang like the above - as it should.- 运行
python -m pytest
将使启动的任何python
都使用其安装中的软件包. - 或者,对于其他Python版本,
pytest
可能会在名为py.test-<version>
的PATH
上包含版本化的可执行文件(例如,py.test-3
或py.test-3.6
),具体取决于安装方式.- 如果它来自系统软件包管理器的非标准python软件包(例如
python36-pytest
),则实际上可以保证. - 我检查了是否使用
pip
安装了一个版本,它只会创建一个未版本控制的可执行文件(尽管您可以自己创建一个版本控制的可执行文件).此外,如果为不同的Python版本安装相同的软件包但使用相同的--prefix
,它将覆盖现有的可执行文件!
- Running
python -m pytest
will make whicheverpython
that launches itself use the package from its installation. - Alternatively,
pytest
for your other Python version may include versioned executables on thePATH
namedpy.test-<version>
(e.g.py.test-3
orpy.test-3.6
) depending on the way you installed it.- If it's from a system package manager's package for nonstandard python - like
python36-pytest
- this is virtually guaranteed. - I checked that if you install a version with
pip
, it only creates an unversioned executable (though you can create a versioned one yourself). Moreover, if you install the same package for a different Python version but with the same--prefix
, it will overwrite the existing one's executable!
- 这不适用于系统的Python,但可以使用默认的
/usr/local
. - 一旦您切换到其他Python版本,它就会声明为 shims 创建一个 shims em>该版本当前安装的所有脚本(包括
pip
!!),因此,调用没有路径的脚本将运行那些补片.- 因此,如果未为替代版本安装软件包(及其脚本),但为系统版本安装了软件包,则尝试运行其可执行文件将陷入"
/usr/local
,仅显示您现在看到的结果
- This is not applicable for the system's Python but the default
/usr/local
can be used for it. - Once you switch to an alternative Python version, it claims to create shims for all scripts (including
pip
!) that are currently installed for that version, so invoking those scripts without a path would run those shims.- So, if a package (and thus its script) is not installed for the alternative version but installed for system version, trying to run its executable would "fall through" to
/usr/local
with just the result you're seeing now.
这篇关于pytest与另一个版本的python一起运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
- So, if a package (and thus its script) is not installed for the alternative version but installed for system version, trying to run its executable would "fall through" to
- 因此,如果未为替代版本安装软件包(及其脚本),但为系统版本安装了软件包,则尝试运行其可执行文件将陷入"
- If it's from a system package manager's package for nonstandard python - like
- 如果它来自系统软件包管理器的非标准python软件包(例如
- since the
- 由于