web-dev-qa-db-fra.com

Python et pip, listent toutes les versions d’un paquet disponible?

Étant donné le nom d'un paquet Python (2.X) pouvant être installé avec pip et virtualenv , existe-t-il un moyen de connaître la liste de toutes les versions possibles de celui-ci que pip pourrait installer? En ce moment, c'est un essai et une erreur.

J'essaie d'installer une version pour une bibliothèque tierce, mais la version la plus récente est trop récente, des modifications incompatibles ont été apportées. Je voudrais donc avoir en quelque sorte une liste de toutes les versions connues de pip, afin que je puisse les tester.

315
Rory

Le script chez Pastebin fonctionne. Cependant, ce n'est pas très pratique si vous travaillez avec plusieurs environnements/hôtes, car vous devrez le copier/créer à chaque fois.

Une meilleure solution globale consisterait à utiliser jaune , qui peut être installé avec pip. Par exemple. pour voir quelles versions de Django sont disponibles:

$ 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

Un avertissement mineur: le jaune dépend de distribuer. Ce n'est pas une mauvaise chose, mais cela peut poser problème si, pour une raison quelconque, vous devez vous en tenir à (obsolète) python setuptools.

Note: Je ne suis pas impliqué dans le développement du jaune. Si quelque chose ne semble pas fonctionner comme il se doit, laisser un commentaire ici ne devrait pas faire beaucoup de différence. Utilisez plutôt le suivi des problèmes de jaune et envisagez de soumettre un correctif, si possible.

152
m000

Pour pip> = 9.0 use

$ 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==

- toutes les versions disponibles seront imprimées sans télécharger ni installer de paquet supplémentaire.

Pour pip <9.0 use

pip install pylibmc==blork

blork peut être une chaîne qui n'est pas susceptible d'être un candidat à l'installation.

563
Chris Montanaro

Mettre à jour:
Depuis septembre 2017, cette méthode ne fonctionne plus: --no-install a été supprimé du pip 7

Utilisez pip install -v, vous pouvez voir toutes les versions disponibles

root@node7:~# pip install web.py -v
Downloading/unpacking web.py
  Using version 0.37 (newest of versions: 0.37, 0.36, 0.35, 0.34, 0.33, 0.33, 0.32, 0.31, 0.22, 0.2)
  Downloading web.py-0.37.tar.gz (90Kb): 90Kb downloaded
  Running setup.py Egg_info for package web.py
    running Egg_info
    creating pip-Egg-info/web.py.Egg-info

Pour ne pas installer de paquet, utilisez l’une des solutions suivantes:

root@node7:~# pip install --no-deps --no-install flask -v                                                                                                      
Downloading/unpacking flask
  Using version 0.10.1 (newest of versions: 0.10.1, 0.10, 0.9, 0.8.1, 0.8, 0.7.2, 0.7.1, 0.7, 0.6.1, 0.6, 0.5.2, 0.5.1, 0.5, 0.4, 0.3.1, 0.3, 0.2, 0.1)
  Downloading Flask-0.10.1.tar.gz (544Kb): 544Kb downloaded

ou 

root@node7:~# cd $(mktemp -d)
root@node7:/tmp/tmp.c6H99cWD0g# pip install flask -d . -v
Downloading/unpacking flask
  Using version 0.10.1 (newest of versions: 0.10.1, 0.10, 0.9, 0.8.1, 0.8, 0.7.2, 0.7.1, 0.7, 0.6.1, 0.6, 0.5.2, 0.5.1, 0.5, 0.4, 0.3.1, 0.3, 0.2, 0.1)
  Downloading Flask-0.10.1.tar.gz (544Kb): 4.1Kb downloaded

Testé avec pip 1.0

root@node7:~# pip --version
pip 1.0 from /usr/lib/python2.7/dist-packages (python 2.7)
70
HVNSweeting

Vous n'avez pas besoin d'un package tiers pour obtenir ces informations. pypi fournit des flux JSON simples pour tous les packages de

https://pypi.python.org/pypi/{PKG_NAME}/json

Voici du code Python utilisant uniquement la bibliothèque standard qui obtient toutes les versions.

import json
import urllib2
from distutils.version import StrictVersion

def versions(package_name):
    url = "https://pypi.python.org/pypi/%s/json" % (package_name,)
    data = json.load(urllib2.urlopen(urllib2.Request(url)))
    versions = data["releases"].keys()
    versions.sort(key=StrictVersion)
    return versions

print "\n".join(versions("scikit-image"))

Ce code imprime (à partir du 23 février 2015):

0.7.2
0.8.0
0.8.1
0.8.2
0.9.0
0.9.1
0.9.2
0.9.3
0.10.0
0.10.1
47
eric chiang

Vous pourriez utiliser le paquet yolk3k au lieu de yolk. yolk3k est une fourchette du jaune d'origine et prend en charge python2 et 3.

https://github.com/myint/yolk

pip install yolk3k
18
ykyuen

Après avoir examiné le code de pip pendant un moment, il semble que le code responsable de la localisation des paquetages se trouve dans la classe PackageFinder de pip.index. Sa méthode find_requirement recherche les versions d'une InstallRequirement, mais ne renvoie malheureusement que la version la plus récente. 

Le code ci-dessous correspond presque à une copie 1: 1 de la fonction d'origine. Le retour à la ligne 114 a été modifié pour renvoyer toutes les versions.

Le script attend un nom de package en tant que premier et unique argument et renvoie toutes les versions.

http://Pastebin.com/axzdUQhZ

Je ne peux pas garantir l'exactitude, car je ne connais pas le code de pip. Mais j'espère que cela aide.

Échantillon de sortie

$ 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

Le code:

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 __== '__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]
15
Reiner Gerecke

Je suis venu avec le script bash dead-simple. Merci à l'auteur de jq .

#!/bin/bash
set -e

PACKAGE_JSON_URL="https://pypi.python.org/pypi/${1}/json"

curl -s "$PACKAGE_JSON_URL" | jq  -r '.releases | keys | .[]' | sort -V

Update: Ajouter un tri par numéro de version.

14
Timofey Stolbov

https://pypi.python.org/pypi/Django/ - fonctionne pour les paquetages dont les responsables choisissent d'afficher tous les paquets https://pypi.python.org/simple/pip/ - devrait faire l'affaire de toute façon (liste tous les liens)

8
m0she

Je sais que c'est un peu ridicule, mais vous pouvez essayer quelque chose comme ceci:

pip install Django == x

Cela provoquera une erreur, mais listera toutes les versions disponibles pour ce paquet.

Il suffit de remplacer Django par le paquet que vous voulez et je pense aussi qu’il n’existe pas de version nommée x.

5
ian0411

Je n'ai pas eu de chance avec yolk, yolk3k ou pip install -v mais je me suis donc retrouvé avec ceci (adapté à Python 3 d'après la réponse d'eric chiang):

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
...
3
Andrew Magee

Pip 7.1.0 a supprimé l'option --no-install de install. J'ai trouvé une méthode pour obtenir toutes les versions d'un paquet sans paquet supplémentaire.

$ pip install --no-deps Django==x.x.x
Collecting Django==x.x.x.
Could not find a version that satisfies the requirement Django==x.x.x. (from versions: 1.1.3, 1.1.4, 1.2, 1.2.1, 1.2.2, 1.2.3, 1.2.4, 1.2.5, 1.2.6, 1.2.7, 1.3, 1.3.1, 1.3.2, 1.3.3, 1.3.4, 1.3.5, 1.3.6, 1.3.7, 1.4, 1.4.1, 1.4.2, 1.4.3, 1.4.4, 1.4.5, 1.4.6, 1.4.7, 1.4.8, 1.4.9, 1.4.10, 1.4.11, 1.4.12, 1.4.13, 1.4.14, 1.4.15, 1.4.16, 1.4.17, 1.4.18, 1.4.19, 1.4.20, 1.5, 1.5.1, 1.5.2, 1.5.3, 1.5.4, 1.5.5, 1.5.6, 1.5.7, 1.5.8, 1.5.9, 1.5.10, 1.5.11, 1.5.12, 1.6, 1.6.1, 1.6.2, 1.6.3, 1.6.4, 1.6.5, 1.6.6, 1.6.7, 1.6.8, 1.6.9, 1.6.10, 1.6.11, 1.7, 1.7.1, 1.7.2, 1.7.3, 1.7.4, 1.7.5, 1.7.6, 1.7.7, 1.7.8, 1.8a1, 1.8b1, 1.8b2, 1.8rc1, 1.8, 1.8.1, 1.8.2)
No matching distribution found for Django==x.x.x.
3
samsong8610

Vous pouvez utiliser ce court extrait de code Python3 pour récupérer la liste des versions disponibles pour un package à partir de PyPI . Contrairement à d'autres solutions Python publiées ici, cela ne se produit pas sur les versions non fixes comme Django's 1.10rc1 ou uwsgi's 2.0.13.1:

>>> import requests
>>> from pkg_resources import parse_version
>>> 
>>> def versions(name):
...     url = "https://pypi.python.org/pypi/{}/json".format(name)
...     return sorted(requests.get(url).json()["releases"], key=parse_version)
... 
>>> print(*reversed(versions("Django")), sep="\n")
1.10.3
1.10.2
1.10.1
1.10
1.10rc1
1.10b1
1.10a1
...
2
Eugene Yarmash

Je viens de courir ceci:

pip show packagename

par exemple.:

> pip3 show setuptools
---
Metadata-Version: 2.0
Name: setuptools
Version: 18.4
Summary: Easily download, build, install, upgrade, and uninstall Python packages
Home-page: https://bitbucket.org/pypa/setuptools
Author: Python Packaging Authority
Author-email: [email protected]
License: PSF or ZPL
Location: /usr/local/lib/python3.4/site-packages
Requires: 
1
Peter Ehrlich

Mon point de vue est une combinaison de quelques réponses postées, avec quelques modifications pour les rendre plus faciles à utiliser à partir d’un environnement Python en cours d’exécution.

L'idée est de fournir une commande entièrement nouvelle (calquée sur la commande install) qui vous donne une instance du package Finder à utiliser. L’avantage, c’est que cela fonctionne avec, et utilise, tous les index pris en charge par pip et lit vos fichiers de configuration de pip locaux, afin que vous obteniez les résultats corrects comme vous le feriez avec une installation normale de pip.

J'ai essayé de le rendre compatible à la fois avec pip v 9.x et 10.x .. mais je ne l'ai essayé que sur 9.x

https://Gist.github.com/kaos/68511bd013fcdebe766c981f50b473d4

#!/usr/bin/env python
# When you want a easy way to get at all (or the latest) version of a certain python package from a PyPi index.

import sys
import logging

try:
    from pip._internal import cmdoptions, main
    from pip._internal.commands import commands_dict
    from pip._internal.basecommand import RequirementCommand
except ImportError:
    from pip import cmdoptions, main
    from pip.commands import commands_dict
    from pip.basecommand import RequirementCommand

from pip._vendor.packaging.version import parse as parse_version

logger = logging.getLogger('pip')

class ListPkgVersionsCommand(RequirementCommand):
    """
    List all available versions for a given package from:

    - PyPI (and other indexes) using requirement specifiers.
    - VCS project urls.
    - Local project directories.
    - Local or remote source archives.

    """
    name = "list-pkg-versions"
    usage = """
      %prog [options] <requirement specifier> [package-index-options] ...
      %prog [options] [-e] <vcs project url> ...
      %prog [options] [-e] <local project path> ...
      %prog [options] <archive url/path> ..."""

    summary = 'List package versions.'

    def __init__(self, *args, **kw):
        super(ListPkgVersionsCommand, self).__init__(*args, **kw)

        cmd_opts = self.cmd_opts

        cmd_opts.add_option(cmdoptions.install_options())
        cmd_opts.add_option(cmdoptions.global_options())
        cmd_opts.add_option(cmdoptions.use_wheel())
        cmd_opts.add_option(cmdoptions.no_use_wheel())
        cmd_opts.add_option(cmdoptions.no_binary())
        cmd_opts.add_option(cmdoptions.only_binary())
        cmd_opts.add_option(cmdoptions.pre())
        cmd_opts.add_option(cmdoptions.require_hashes())

        index_opts = cmdoptions.make_option_group(
            cmdoptions.index_group,
            self.parser,
        )

        self.parser.insert_option_group(0, index_opts)
        self.parser.insert_option_group(0, cmd_opts)

    def run(self, options, args):
        cmdoptions.resolve_wheel_no_use_binary(options)
        cmdoptions.check_install_build_global(options)

        with self._build_session(options) as session:
            Finder = self._build_package_Finder(options, session)

            # do what you please with the Finder object here... ;)
            for pkg in args:
                logger.info(
                    '%s: %s', pkg,
                    ', '.join(
                        sorted(
                            set(str(c.version) for c in Finder.find_all_candidates(pkg)),
                            key=parse_version,
                        )
                    )
                )


commands_dict[ListPkgVersionsCommand.name] = ListPkgVersionsCommand

if __== '__main__':
    sys.exit(main())

Exemple de sortie

$ ./list-pkg-versions.py list-pkg-versions pika Django
pika: 0.5, 0.5.1, 0.5.2, 0.9.1a0, 0.9.2a0, 0.9.3, 0.9.4, 0.9.5, 0.9.6, 0.9.7, 0.9.8, 0.9.9, 0.9.10, 0.9.11, 0.9.12, 0.9.13, 0.9.14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3, 1.11.4, 1.11.5, 1.11.6, 1.11.7, 1.11.8, 1.11.9, 1.11.10, 1.11.11, 1.11.12, 2.0, 2.0.1, 2.0.2, 2.0.3, 2.0.4
0
Kaos

Voici la méthode actuelle basée sur le pip Python, qui recherche dans l'API de package PyPi existante:

from pip import index
import requests
Finder = index.PackageFinder(
    [],
    ['https://pypi.python.org/simple'],
    session=requests.Session()
)
results = Finder.find_all_candidates("package_name")
versions = [p.version for p in results]
0
benjaoming

La solution alternative consiste à utiliser les API Warehouse:

https://warehouse.readthedocs.io/api-reference/json/#release

Par exemple pour Flask:

import requests
r = requests.get("https://pypi.org/pypi/Flask/json")
print(r.json()['releases'].keys())

imprimera:

dict_keys(['0.1', '0.10', '0.10.1', '0.11', '0.11.1', '0.12', '0.12.1', '0.12.2', '0.12.3', '0.12.4', '0.2', '0.3', '0.3.1', '0.4', '0.5', '0.5.1', '0.5.2', '0.6', '0.6.1', '0.7', '0.7.1', '0.7.2', '0.8', '0.8.1', '0.9', '1.0', '1.0.1', '1.0.2'])
0
Charlie