给定可以与pip一起安装的Python包的名称,是否有任何方法可以找到pip可以安装的所有可能版本的列表?现在是反复试验。
我正在尝试为第三方库安装一个版本,但最新版本太新了,有向后不兼容的更改。因此,我希望以某种方式获得pip所知道的所有版本的列表,以便我可以测试它们。
给定可以与pip一起安装的Python包的名称,是否有任何方法可以找到pip可以安装的所有可能版本的列表?现在是反复试验。
我正在尝试为第三方库安装一个版本,但最新版本太新了,有向后不兼容的更改。因此,我希望以某种方式获得pip所知道的所有版本的列表,以便我可以测试它们。
当前回答
我没有任何蛋黄,yolk3k或pip install -v的运气,但我最终使用了这个(从eric chiang的答案改编到Python 3):
import json
import requests
from distutils.version import StrictVersion
def versions(package_name):
url = "https://pypi.python.org/pypi/{}/json".format(package_name)
data = requests.get(url).json()
return sorted(list(data["releases"].keys()), key=StrictVersion, reverse=True)
>>> print("\n".join(versions("gunicorn")))
19.1.1
19.1.0
19.0.0
18.0
17.5
0.17.4
0.17.3
...
其他回答
pypi-has() { set -o pipefail; curl -sfL https://pypi.org/pypi/$1/json | jq -e --arg v $2 'any( .releases | keys[]; . == $v )'; }
用法:
$ pypi-has django 4.0x ; echo $?
false
1
$ pypi-has djangos 4.0x ; echo $?
22
$ pypi-has djangos 4.0 ; echo $?
22
$ pypi-has django 4.0 ; echo $?
true
0
(更新:截至2020年3月,许多人报告说,通过pip install yolk3k安装的蛋黄只返回最新版本。克里斯的回答似乎获得了最多的赞,对我来说很有用)
在pastebin中的脚本可以工作。但是,如果您使用多个环境/主机,这就不太方便了,因为每次都必须复制/创建它。
更好的全面解决方案是使用yolk3k,它可以与pip一起安装。例如,查看Django的可用版本:
$ pip install yolk3k
$ yolk -V django
Django 1.3
Django 1.2.5
Django 1.2.4
Django 1.2.3
Django 1.2.2
Django 1.2.1
Django 1.2
Django 1.1.4
Django 1.1.3
Django 1.1.2
Django 1.0.4
Yolk3k是原蛋黄的一个分支,于2012年停止开发。虽然不再维护蛋黄(如下面的评论所示),但蛋黄3k似乎是并支持Python 3。
注意:我没有参与蛋黄3k的形成。如果有些东西似乎没有像它应该的那样工作,在这里留下评论应该不会有太大的区别。如果可能的话,请使用yolk3k问题跟踪器,并考虑提交修复程序。
在看了一段时间pip的代码之后,负责定位包的代码似乎可以在pip.index中的PackageFinder类中找到。它的方法find_requirement查找InstallRequirement的版本,但不幸的是只返回最新的版本。
下面的代码几乎是原始函数的1:1副本,只是第114行中的返回值更改为返回所有版本。
脚本期望一个包名作为第一个也是唯一的参数,并返回所有版本。
http://pastebin.com/axzdUQhZ
我不能保证正确,因为我不熟悉pip的代码。但希望这能帮到你。
样例输出
python test.py pip
Versions of pip
0.8.2
0.8.1
0.8
0.7.2
0.7.1
0.7
0.6.3
0.6.2
0.6.1
0.6
0.5.1
0.5
0.4
0.3.1
0.3
0.2.1
0.2 dev
代码:
import posixpath
import pkg_resources
import sys
from pip.download import url_to_path
from pip.exceptions import DistributionNotFound
from pip.index import PackageFinder, Link
from pip.log import logger
from pip.req import InstallRequirement
from pip.util import Inf
class MyPackageFinder(PackageFinder):
def find_requirement(self, req, upgrade):
url_name = req.url_name
# Only check main index if index URL is given:
main_index_url = None
if self.index_urls:
# Check that we have the url_name correctly spelled:
main_index_url = Link(posixpath.join(self.index_urls[0], url_name))
# This will also cache the page, so it's okay that we get it again later:
page = self._get_page(main_index_url, req)
if page is None:
url_name = self._find_url_name(Link(self.index_urls[0]), url_name, req) or req.url_name
# Combine index URLs with mirror URLs here to allow
# adding more index URLs from requirements files
all_index_urls = self.index_urls + self.mirror_urls
def mkurl_pypi_url(url):
loc = posixpath.join(url, url_name)
# For maximum compatibility with easy_install, ensure the path
# ends in a trailing slash. Although this isn't in the spec
# (and PyPI can handle it without the slash) some other index
# implementations might break if they relied on easy_install's behavior.
if not loc.endswith('/'):
loc = loc + '/'
return loc
if url_name is not None:
locations = [
mkurl_pypi_url(url)
for url in all_index_urls] + self.find_links
else:
locations = list(self.find_links)
locations.extend(self.dependency_links)
for version in req.absolute_versions:
if url_name is not None and main_index_url is not None:
locations = [
posixpath.join(main_index_url.url, version)] + locations
file_locations, url_locations = self._sort_locations(locations)
locations = [Link(url) for url in url_locations]
logger.debug('URLs to search for versions for %s:' % req)
for location in locations:
logger.debug('* %s' % location)
found_versions = []
found_versions.extend(
self._package_versions(
[Link(url, '-f') for url in self.find_links], req.name.lower()))
page_versions = []
for page in self._get_pages(locations, req):
logger.debug('Analyzing links from page %s' % page.url)
logger.indent += 2
try:
page_versions.extend(self._package_versions(page.links, req.name.lower()))
finally:
logger.indent -= 2
dependency_versions = list(self._package_versions(
[Link(url) for url in self.dependency_links], req.name.lower()))
if dependency_versions:
logger.info('dependency_links found: %s' % ', '.join([link.url for parsed, link, version in dependency_versions]))
file_versions = list(self._package_versions(
[Link(url) for url in file_locations], req.name.lower()))
if not found_versions and not page_versions and not dependency_versions and not file_versions:
logger.fatal('Could not find any downloads that satisfy the requirement %s' % req)
raise DistributionNotFound('No distributions at all found for %s' % req)
if req.satisfied_by is not None:
found_versions.append((req.satisfied_by.parsed_version, Inf, req.satisfied_by.version))
if file_versions:
file_versions.sort(reverse=True)
logger.info('Local files found: %s' % ', '.join([url_to_path(link.url) for parsed, link, version in file_versions]))
found_versions = file_versions + found_versions
all_versions = found_versions + page_versions + dependency_versions
applicable_versions = []
for (parsed_version, link, version) in all_versions:
if version not in req.req:
logger.info("Ignoring link %s, version %s doesn't match %s"
% (link, version, ','.join([''.join(s) for s in req.req.specs])))
continue
applicable_versions.append((link, version))
applicable_versions = sorted(applicable_versions, key=lambda v: pkg_resources.parse_version(v[1]), reverse=True)
existing_applicable = bool([link for link, version in applicable_versions if link is Inf])
if not upgrade and existing_applicable:
if applicable_versions[0][1] is Inf:
logger.info('Existing installed version (%s) is most up-to-date and satisfies requirement'
% req.satisfied_by.version)
else:
logger.info('Existing installed version (%s) satisfies requirement (most up-to-date version is %s)'
% (req.satisfied_by.version, applicable_versions[0][1]))
return None
if not applicable_versions:
logger.fatal('Could not find a version that satisfies the requirement %s (from versions: %s)'
% (req, ', '.join([version for parsed_version, link, version in found_versions])))
raise DistributionNotFound('No distributions matching the version for %s' % req)
if applicable_versions[0][0] is Inf:
# We have an existing version, and its the best version
logger.info('Installed version (%s) is most up-to-date (past versions: %s)'
% (req.satisfied_by.version, ', '.join([version for link, version in applicable_versions[1:]]) or 'none'))
return None
if len(applicable_versions) > 1:
logger.info('Using version %s (newest of versions: %s)' %
(applicable_versions[0][1], ', '.join([version for link, version in applicable_versions])))
return applicable_versions
if __name__ == '__main__':
req = InstallRequirement.from_line(sys.argv[1], None)
finder = MyPackageFinder([], ['http://pypi.python.org/simple/'])
versions = finder.find_requirement(req, False)
print 'Versions of %s' % sys.argv[1]
for v in versions:
print v[1]
使用pip install <package_name>==为Chris的答案提供编程方法
import re
import subprocess
from packaging.version import VERSION_PATTERN as _VRESION_PATTERN
VERSION_PATTERN = re.compile(_VRESION_PATTERN , re.VERBOSE | re.IGNORECASE)
def get_available_versions(package_name):
process = subprocess.run(['pip', 'install', f'{package_name}=='], stdout=subprocess.DEVNULL, stderr=subprocess.PIPE)
versions = []
for line in process.stderr.decode('utf-8').splitlines():
if 'Could not find a version that satisfies the requirement' in line:
for match in VERSION_PATTERN.finditer(line.split('from versions:')[1]):
versions.append(match.group(0))
return versions
它可以像这样使用
>>> get_available_versions('tensorflow')
['2.2.0rc1', '2.2.0rc2', '2.2.0rc3', '2.2.0rc4', '2.2.0', '2.2.1', '2.2.2', '2.2.3', '2.3.0rc0', '2.3.0rc1', '2.3.0rc2', '2.3.0', '2.3.1', '2.3.2', '2.3.3', '2.3.4', '2.4.0rc0', '2.4.0rc1', '2.4.0rc2', '2.4.0rc3', '2.4.0rc4', '2.4.0', '2.4.1', '2.4.2', '2.4.3', '2.4.4', '2.5.0rc0', '2.5.0rc1', '2.5.0rc2', '2.5.0rc3', '2.5.0', '2.5.1', '2.5.2', '2.5.3', '2.6.0rc0', '2.6.0rc1', '2.6.0rc2', '2.6.0', '2.6.1', '2.6.2', '2.6.3', '2.7.0rc0', '2.7.0rc1', '2.7.0', '2.7.1', '2.8.0rc0', '2.8.0rc1', '2.8.0']
并返回一个版本列表。
注意:它似乎提供兼容版本,而不是所有版本。要获得完整的列表,请使用Eric的json方法。
对于pip >= 21.2使用:
pip index versions pylibmc
注意,这个命令是实验性的,将来可能会改变!
对于pip >= 21.1使用:
pip install pylibmc==
对于pip >= 20.3使用:
pip install --use-deprecated=legacy-resolver pylibmc==
对于pip >= 9.0使用:
$ pip install pylibmc==
Collecting pylibmc==
Could not find a version that satisfies the requirement pylibmc== (from
versions: 0.2, 0.3, 0.4, 0.5.1, 0.5.2, 0.5.3, 0.5.4, 0.5.5, 0.5, 0.6.1, 0.6,
0.7.1, 0.7.2, 0.7.3, 0.7.4, 0.7, 0.8.1, 0.8.2, 0.8, 0.9.1, 0.9.2, 0.9,
1.0-alpha, 1.0-beta, 1.0, 1.1.1, 1.1, 1.2.0, 1.2.1, 1.2.2, 1.2.3, 1.3.0)
No matching distribution found for pylibmc==
可用的版本将在不实际下载或安装任何软件包的情况下打印出来。
对于pip < 9.0使用:
pip install pylibmc==blork
其中,block可以是任何不是有效版本号的字符串。