如何使用 pip 列出所有可用的软件包版本?
给定可以使用 pip 安装的 Python 包的名称,有什么方法可以找到列出 pip 可以安装的所有可能版本?现在正在尝试和错误。
我正在尝试安装第三方库的版本,但最新版本太新,做出了向后不兼容的更改。所以我想以某种方式拥有 pip 知道的所有版本的列表,以便我可以测试它们。
Given the name of a Python package that can be installed with pip, is there any way to find out a list of all the possible versions of it that pip could install? Right now it's trial and error.
I'm trying to install a version for a third party library, but the newest version is too new, there were backwards incompatible changes made. So I'd like to somehow have a list of all the versions that pip knows about, so that I can test them.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
对于 pip >= 21.2 使用:
请注意,此命令是实验性的,将来可能会发生变化!
以下方法与 2024 年 6 月 21 日发布的 pip 24.1 不同。
对于 pip >= 21.1 使用:
对于 pip >= 20.3 使用:
对于 pip >= 9.0 使用:
可用版本为无需实际下载或安装任何软件包即可打印。
对于点 9.0 使用:
其中
blork
可以是任何 不是有效的版本号。For pip >= 21.2 use:
Note that this command is experimental, and might change in the future!
Below approach breaks with pip 24.1 released on 2024-06-21.
For pip >= 21.1 use:
For pip >= 20.3 use:
For pip >= 9.0 use:
The available versions will be printed without actually downloading or installing any packages.
For pip < 9.0 use:
where
blork
can be any string that is not a valid version number.(更新:截至 2020 年 3 月,许多人报告通过
pip install yolk3k
安装的 yolk 仅返回最新版本。克里斯的回答似乎得到了最多的支持并且对我有用)pastebin的脚本确实有效。但是,如果您正在使用多个环境/主机,则不太方便,因为您每次都必须复制/创建它。
更好的全能解决方案是使用 yolk3k,它可以通过 pip 安装。例如,查看可用的 Django 版本:
yolk3k
是原始的分支yolk
于 2012 停止开发。虽然yolk
不再维护(如下面的评论所示),但yolk3k
似乎是并支持 Python 3。注意: 我不参与在 yolk3k 的开发中。 如果某些东西似乎无法正常工作,在这里留下评论应该不会有太大影响。使用yolk3k 问题跟踪器 相反,并考虑提交修复(如果可能)。
(update: As of March 2020, many people have reported that yolk, installed via
pip install yolk3k
, only returns latest version. Chris's answer seems to have the most upvotes and worked for me)The script at pastebin does work. However it's not very convenient if you're working with multiple environments/hosts because you will have to copy/create it every time.
A better all-around solution would be to use yolk3k, which is available to install with pip. E.g. to see what versions of Django are available:
yolk3k
is a fork of the originalyolk
which ceased development in 2012. Thoughyolk
is no longer maintained (as indicated in comments below),yolk3k
appears to be and supports Python 3.Note: I am not involved in the development of yolk3k. If something doesn't seem to work as it should, leaving a comment here should not make much difference. Use the yolk3k issue tracker instead and consider submitting a fix, if possible.
您不需要第三方包来获取此信息。 pypi 为下面的所有包提供简单的 JSON 源,
这是一些仅使用获取所有版本的标准库的 Python 代码。
该代码打印(截至 2023 年 7 月 21 日):
You don't need a third party package to get this information. pypi provides simple JSON feeds for all packages under
Here's some Python code using only the standard library which gets all versions.
That code prints (as of 21/Jul/2023):
更新:
截至 2017 年 9 月,此方法不再有效:
--no-install
已在 pip 7 中删除使用
pip install -v
,您可以看到所有可用版本要不安装任何软件包,请使用以下解决方案之一:
或
使用 pip 1.0 进行测试
Update:
As of Sep 2017 this method no longer works:
--no-install
was removed in pip 7Use
pip install -v
, you can see all versions that availableTo not install any package, use one of following solution:
or
Tested with pip 1.0
我想出了一个极其简单的 bash 脚本。感谢 jq 的作者。
I came up with dead-simple bash script. Thanks to jq's author.
您可以使用这个小型 Python 3 脚本(仅使用标准库模块)使用 JSON API 并按时间倒序打印它们。与此处发布的其他一些 Python 解决方案不同,这不会在
django
的2.2rc1
或uwsgi
的等松散版本上中断。 2.0.17.1
:保存脚本并以包名称作为参数运行它,例如:
You can use this small Python 3 script (using only standard library modules) to grab the list of available versions for a package from PyPI using JSON API and print them in reverse chronological order. Unlike some other Python solutions posted here, this doesn't break on loose versions like
django
's2.2rc1
oruwsgi
's2.0.17.1
:Save the script and run it with the package name as an argument, e.g.:
看了一会儿pip的代码,貌似负责定位包的代码可以在
pip.index
中的PackageFinder
类中找到。它的方法find_requirement
查找InstallRequirement
的版本,但遗憾的是仅返回最新版本。下面的代码几乎是原始函数的 1:1 副本,其中第 114 行的 return 更改为返回所有版本。
该脚本需要一个包名称作为第一个也是唯一的参数,并返回所有版本。
http://pastebin.com/axzdUQhZ
我不能保证正确性,因为我不熟悉 pip 的代码。但希望这会有所帮助。
示例输出
代码:
After looking at pip's code for a while, it looks like the code responsible for locating packages can be found in the
PackageFinder
class inpip.index
. Its methodfind_requirement
looks up the versions of aInstallRequirement
, but unfortunately only returns the most recent version.The code below is almost a 1:1 copy of the original function, with the return in line 114 changed to return all versions.
The script expects one package name as first and only argument and returns all versions.
http://pastebin.com/axzdUQhZ
I can't guarantee for the correctness, as I'm not familiar with pip's code. But hopefully this helps.
Sample output
The code:
你可以用 yolk3k 包代替蛋黄。 yolk3k 是原始 yolk 的一个分支,它支持 python2 和 3。
https://github.com/myint/蛋黄
You could the yolk3k package instead of yolk. yolk3k is a fork from the original yolk and it supports both python2 and 3.
https://github.com/myint/yolk
我的项目
luddite
有这个功能。用法示例:
通过查询 JSON API 列出可用包的所有版本href="https://pypi.org/" rel="noreferrer">https://pypi.org/
My project
luddite
has this feature.Example usage:
It lists all versions of a package available, by querying the JSON API of https://pypi.org/
更新:
也许不再需要该解决方案,请检查对此答案的评论。
原始答案
对于 20.03 以上的 pip 版本,您可以使用旧的求解器来恢复所有可用版本:
Update:
Maybe the solution is not needed anymore, check comments to this answer.
Original Answer
With pip versions above 20.03 you can use the old solver in order to get back all the available versions:
这在 OSX 上适用于我:
它每行返回一个列表:
或者获取可用的最新版本:
请记住,必须安装(在 OSX 上)
gsort
才能解析版本。您可以使用brew install coreutils
安装它This works for me on OSX:
It returns the list one per line:
Or to get the latest version available:
Keep in mind
gsort
has to be installed (on OSX) to parse the versions. You can install it withbrew install coreutils
我通常运行
pip install packagename==somerandomstring
。这会返回错误,指出无法找到满足要求的版本 packagename==somerandomstring
并且除了该错误之外,pip 还将列出服务器上的可用版本。例如
,如果像“aksjflashd”这样的随机字符串结果是实际的软件包版本,那么您一定非常不幸!
当然,您也可以在
pip download
中使用此技巧。I usually run
pip install packagename==somerandomstring
. This returns error sayingCould not find a version that satisfies the requirement packagename==somerandomstring
and along with that error, pip will also list available versions on the server.e.g.
You have to be extremely unlucky if the random string like 'aksjflashd' turns out to be actual package version!
Of course, you can use this trick with
pip download
too.pip-versions 包做得非常出色:
这甚至可以在 Nexus (sonatype ) 代理人!
The pip-versions package does an excellent job:
And this even works behind a Nexus (sonatype) proxy!
替代解决方案是使用 Warehouse API:
https://warehouse.readthedocs.io/api -reference/json/#release
例如对于 Flask:
将打印:
Alternative solution is to use the Warehouse APIs:
https://warehouse.readthedocs.io/api-reference/json/#release
For instance for Flask:
will print:
这是我的答案,它对
jq
内的列表进行排序(对于那些使用sort -V
不可用的系统的人):并获取包的最后版本号:
或更快一点:
或者更简单:):
Here's my answer that sorts the list inside
jq
(for those who use systems wheresort -V
is not avalable) :And to fetch the last version number of the package :
or a bit faster :
Or even more simple :) :
简单的 bash 脚本仅依赖于 python 本身(我假设在问题的上下文中应该安装它)和 curl 或之一
wget
。它假设您安装了 setuptools 软件包来对版本进行排序(几乎总是安装)。它不依赖于外部依赖项,例如jq
;grep
和awk
在 Linux 和 macOS 上的行为可能有所不同。带有注释的较长版本。
将包名称放入变量中:
获取版本(使用
curl
):获取版本(使用
wget
) :打印排序版本:
Simple
bash
script that relies only onpython
itself (I assume that in the context of the question it should be installed) and one ofcurl
orwget
. It has an assumption that you havesetuptools
package installed to sort versions (almost always installed). It doesn't rely on external dependencies such as:jq
which may not be present;grep
andawk
that may behave differently on Linux and macOS.A little bit longer version with comments.
Put the package name into a variable:
Get versions (using
curl
):Get versions (using
wget
):Print sorted versions:
适用于最新的 pip 版本,无需额外工具:
Works with recent pip versions, no extra tools necessary:
要查找所有可用的(甚至不兼容)版本,请使用
-vv
标志和pip >= 21.x
。不兼容的版本将在日志中列出,如下所示:
To find all available (even incompatible) versions as well, use the
-vv
flag withpip >= 21.x
.Incompatible versions will be listed in the log like this:
我对
yolk
、yolk3k
或pip install -v
没有任何运气,但所以我最终使用了这个(适应于 Python 3) eric chiang的回答):I didn't have any luck with
yolk
,yolk3k
orpip install -v
but so I ended up using this (adapted to Python 3 from eric chiang's answer):现在获取最新依赖版本的最简单方法是使用curl和jq:
The easiest way to get the latest dependency version nowadays, using just curl and jq:
我写了一个函数来为我解决这个问题。希望有帮助。
I wrote a function to solve this problem for me. Hope it helps.
这是Py3.9+版本的Limmy+EricChiang的解决方案。
This is Py3.9+ version of Limmy+EricChiang 's solution.
要获取 GitLab 私有包的最新版本,请执行以下操作。
To fetch the latest version for a GitLab private package, the below works.
据我了解 this 和 这个,
releases
键将在不久的将来被删除,因此使用GET "https://pypi.python.org/pypi/{package_name}/json"
的解决方案将不再有效。As of what I understood from this and this, the
releases
key will be dropped in a near future so solutions usingGET "https://pypi.python.org/pypi/{package_name}/json"
will not work anymore.在
pip
的更新版本中,以下解决方案不起作用:请在
==
之后使用任何随机数,而不是这些解决方案,如下所示:要纠正您,终端将显示所有可用版本。您可以选择适合您的版本。
In the updated version of
pip
the following solution doesn't work:Instead of those, use any random number after the
==
, just like the following:To correct you, the terminal will show you all the available versions. You can choose the right version for you.
如果您想安装名为
pkg
的软件包,只需尝试:这适用于任何 pip 版本。
if you want to install a package named
pkg
, just try:This applies to any pip versions.
我的看法是结合了几个已发布的答案,并进行了一些修改,以使它们更容易在运行的 python 环境中使用。
这个想法是提供一个全新的命令(以 install 命令为模型),为您提供一个要使用的包查找器的实例。优点是它可以与 pip 支持的任何索引配合使用并读取本地 pip 配置文件,因此您可以获得正确的结果,就像正常的 pip 安装一样。
我尝试使其与 pip v 9.x 和 10.x 兼容..但只在 9.x
https://gist.github.com/kaos/68511bd013fcdebe766c981f50b473d4
示例输出
My take is a combination of a couple of posted answers, with some modifications to make them easier to use from within a running python environment.
The idea is to provide a entirely new command (modeled after the install command) that gives you an instance of the package finder to use. The upside is that it works with, and uses, any indexes that pip supports and reads your local pip configuration files, so you get the correct results as you would with a normal pip install.
I've made an attempt at making it compatible with both pip v 9.x and 10.x.. but only tried it on 9.x
https://gist.github.com/kaos/68511bd013fcdebe766c981f50b473d4
Example output
用法:
Usage:
使用
pip install==
为 Chris 的答案 提供编程方法,可以像这样使用
并返回版本列表。
注意:它似乎提供兼容版本而不是所有版本。要获取完整列表,请使用 Eric 的 json 方法。
Providing a programmatic approach to Chris's answer using
pip install <package_name>==
It can be used like
and return a list of versions.
Note: it seems to provide compatible releases rather than all releases. To get full list, use json approach from Eric.