Python: Mettre à jour tous les paquets obsolètes

La commande pip list permet d'afficher la liste complète de tous les paquets python installés avec la version.

# pip list
asn1crypto (0.22.0)
backports.ssl-match-hostname (3.5.0.1)
batinfo (0.4.2)
blessings (1.6)
bottle (0.12.13)
bpython (0.16)
certifi (2017.4.17)
cffi (1.10.0)
chardet (3.0.4)
...
requests (2.18.1)
setuptools (36.2.0)
six (1.10.0)
statsd (3.2.1)
urllib3 (1.21.1)
wcwidth (0.1.7)
websocket-client (0.44.0)
zeroconf (0.19.1)

La commande suivante affiche les principales options disponibles pour la commande pip list:

# pip list -h
Usage:
  pip list [options]

Description:
  List installed packages, including editables.

  Packages are listed in a case-insensitive sorted order.

List Options:
  -o, --outdated              List outdated packages
  -u, --uptodate              List uptodate packages
  -e, --editable              List editable projects.
  -l, --local                 If in a virtualenv that has global access, do not list globally-installed packages.
  --user                      Only output packages installed in user-site.
  --pre                       Include pre-release and development versions. By default, pip only finds stable versions.
  --format <list_format>      Select the output format among: legacy (default), columns, freeze or json.
  --not-required              List packages that are not dependencies of installed packages.

Donc, pour obtenir la liste complète de tous les paquets Python obsolètes, il suffit d'utiliser l'option -o

# pip list -o
Package    Version Latest Type
---------- ------- ------ -----
decorator  4.0.11  4.1.1  wheel
setuptools 36.0.1  36.2.0 wheel

Ou alors directement avec la commande python et le module pip

# python3 -m pip list -o

La commande suivante va effectuer la mise à jour complète des paquets obsolètes.

# for x in $(pip list -o --format=columns | sed -n '3,$p' | cut -d' ' -f1); do pip install $x --upgrade; done
Collecting decorator
  Downloading decorator-4.1.1-py2.py3-none-any.whl
Installing collected packages: decorator
  Found existing installation: decorator 4.0.11
    Uninstalling decorator-4.0.11:
      Successfully uninstalled decorator-4.0.11
Successfully installed decorator-4.1.1
Collecting setuptools
  Downloading setuptools-36.2.0-py2.py3-none-any.whl (477kB)
    100% |████████████████████████████████| 481kB 1.1MB/s
Installing collected packages: setuptools
  Found existing installation: setuptools 36.0.1
    Uninstalling setuptools-36.0.1:
      Successfully uninstalled setuptools-36.0.1
Successfully installed setuptools-36.2.0

Attention à la version de pip utilisée.

En général, pip concerne Python2 et pip3 Python3

Une autre méthode:

$ python3 -m pip list -o > outdated
$ for x in $(sed -n '3,$p' outdated | awk '{print $1}'); do python3 -m pip install --upgrade --user $x; done