如何点安装最小和最大版本范围的软件包?

问题:如何点安装最小和最大版本范围的软件包?

我想知道是否有任何方法可以告诉pip,特别是在需求文件中,安装pip install package>=0.2不应该安装最低版本()和最高版本的软件包(理论api:)pip install package<0.3

我问是因为我正在使用正在积极开发的第三方库。我希望我的pip要求文件指定它应该始终安装0.5.x分支的最新次要版本,但是我不希望pip尝试安装任何更新的主要版本(例如0.6.x),因为API是不同的。这很重要,因为即使0.6.x分支可用,但开发人员仍在向0.5.x分支发布补丁和错误修正,因此我不想package==0.5.9在需求文件中使用静态行。

有什么办法吗?

I’m wondering if there’s any way to tell pip, specifically in a requirements file, to install a package with both a minimum version (pip install package>=0.2) and a maximum version which should never be installed (theoretical api: pip install package<0.3).

I ask because I am using a third party library that’s in active development. I’d like my pip requirements file to specify that it should always install the most recent minor release of the 0.5.x branch, but I don’t want pip to ever try to install any newer major versions (like 0.6.x) since the API is different. This is important because even though the 0.6.x branch is available, the devs are still releasing patches and bugfixes to the 0.5.x branch, so I don’t want to use a static package==0.5.9 line in my requirements file.

Is there any way to do that?


回答 0

你可以做:

$ pip install "package>=0.2,<0.3"

并且pip会寻找最佳匹配,假设版本至少为0.2,且小于0.3。

这也适用于点子要求文件。请参阅PEP 440中有关版本说明符的完整详细信息。

You can do:

$ pip install "package>=0.2,<0.3"

And pip will look for the best match, assuming the version is at least 0.2, and less than 0.3.

This also applies to pip requirements files. See the full details on version specifiers in PEP 440.


回答 1

您还可以使用:

pip install package==0.5.*

更加一致且易于阅读。

you can also use:

pip install package==0.5.*

which is more consistent and easy to read.


回答 2

一种优雅的方法是使用~=符合PEP 440的兼容发布运算符。在您的情况下,总计为:

package~=0.5.0

例如,如果存在以下版本,它将选择0.5.9

  • 0.5.0
  • 0.5.9
  • 0.6.0

为了澄清起见,每对都是等效的:

~= 0.5.0
>= 0.5.0, == 0.5.*

~= 0.5
>= 0.5, == 0.*

An elegant method would be to use the ~= compatible release operator according to PEP 440. In your case this would amount to:

package~=0.5.0

As an example, if the following versions exist, it would choose 0.5.9:

  • 0.5.0
  • 0.5.9
  • 0.6.0

For clarification, each pair is equivalent:

~= 0.5.0
>= 0.5.0, == 0.5.*

~= 0.5
>= 0.5, == 0.*