python执行其它py,pytest与另一个版本的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

I use the following command to switch to python 3.5.2:

pyenv shell 3.5.2

And when I check the python version this is what I get:

$ python --version

Python 3.5.2

But when I run pytest, it still runs under python 2.7.6:

pytest -v

==================================================================== test session starts ====================================================================

platform linux2 -- Python 2.7.6, pytest-3.0.3, py-1.4.31, pluggy-0.4.0 -- /usr/bin/python

Why is pytest running under the older version?

解决方案

Bottom line: run

python -m pytest, or

py.test- if your alternative Python and pytest are installed with system package manager, or

if your alternative Python has been installed with pyenv, switch with pyenv to that version and make sure you have pytest installed for it. Then you can just run pytest.

since the pip executable is also among the ones being switched, you need to switch to the alternative Python before installing pytest for it, too.

As I can see, /usr/bin/pytest (that belongs to the system package manager's python-pytest package) has a shebang !#/usr/bin/python since it corresponds to the system python's installation.

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.

Running python -m pytest will make whichever python that launches itself use the package from its installation.

Alternatively, pytest for your other Python version may include versioned executables on the PATH named py.test- (e.g. py.test-3 or py.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!

pyenv's suggested way seems to be to install all python versions of interest and packages for them under ~/.pyenv/versions.

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值