Categories
pip python

How to upgrade all Python packages with pip

2538

Is it possible to upgrade all Python packages at one time with pip?

Note: that there is a feature request for this on the official issue tracker.

3

  • 62

    Beware software rot—upgrading dependencies might break your app. You can list the exact version of all installed packages with pip freeze (like bundle install or npm shrinkwrap). Best to save a copy of that before tinkering.

    May 22, 2013 at 13:01


  • 3

    If you want to update a single package and all of its dependencies (arguably a more sensible approach), do this: pip install -U –upgrade-strategy eager your-package

    – Cyberwiz

    Feb 24, 2021 at 15:33


  • 8

    I use PowerShell 7 and currently I use this one-liner: pip list --format freeze | %{pip install --upgrade $_.split('==')[0]} (I am unable to post an answer here yet)

    – user15290516

    Mar 7, 2021 at 5:11

2699

There isn’t a built-in flag yet, but you can use:

pip list --outdated --format=freeze | grep -v '^\-e' | cut -d = -f 1  | xargs -n1 pip install -U

For older versions of pip:

pip freeze --local | grep -v '^\-e' | cut -d = -f 1  | xargs -n1 pip install -U

  • The grep is to skip editable (“-e”) package definitions, as suggested by @jawache. (Yes, you could replace grep+cut with sed or awk or perl or…).

  • The -n1 flag for xargs prevents stopping everything if updating one package fails (thanks @andsens).


Note: there are infinite potential variations for this. I’m trying to keep this answer short and simple, but please do suggest variations in the comments!

57

  • 79

    Right 🙁 The issue now lives at github.com/pypa/pip/issues/59 . But every suggestion seems to be answered with “Yeah, but I’m too sure if X is the right way to do Y”… Now is better than never? Practicality beats purity? 🙁

    – rbp

    Aug 12, 2011 at 8:40

  • 27

    It also prints those packages that were installed with a normal package manager (like apt-get or Synaptic). If I execute this pip install -U, it will update all packages. I’m afraid it can cause some conflict with apt-get.

    – Jabba

    Sep 13, 2011 at 4:11

  • 8

    How about changing grep to: egrep -v ‘^(\-e|#)’ (i get this line when running it on ubuntu 12.10: “## FIXME: could not find svn URL in dependency_links for this package:”.

    Mar 5, 2013 at 14:29

  • 40

    I’d throw in a tee before doing the actual upgrade so that you can get a list of the original verisons. E.g. pip freeze --local | tee before_upgrade.txt | ... That way it would be easier to revert if there’s any problems.

    – Emil H

    Mar 4, 2014 at 6:29

  • 11

    I added -H to sudo to avoid an annoying error message: $ pip freeze --local | grep -v '^\-e' | cut -d = -f 1 | xargs -n1 sudo -H pip install -U

    – Mario S

    Mar 17, 2016 at 1:53

793

You can use the following Python code. Unlike pip freeze, this will not print warnings and FIXME errors.
For pip < 10.0.1

import pip
from subprocess import call

packages = [dist.project_name for dist in pip.get_installed_distributions()]
call("pip install --upgrade " + ' '.join(packages), shell=True)

For pip >= 10.0.1

import pkg_resources
from subprocess import call

packages = [dist.project_name for dist in pkg_resources.working_set]
call("pip install --upgrade " + ' '.join(packages), shell=True)

24

  • 28

    This works amazingly well… It’s always so satisfying when a task takes a REALLY long time… and gives you a bunch of new stuff! PS: Run it as root if you’re on OS X!

    – Alex Gray

    Dec 31, 2011 at 4:13


  • 58

    Is there no way to install using pip without calling a subprocess? Something like import pip pip.install('packagename')?

    – endolith

    Mar 6, 2012 at 16:18

  • 6

    I wrapped this up in a fabfile.py. Thanks!

    – Josh K

    Apr 29, 2013 at 21:54

  • 7

    @BenMezger: You really shouldn’t be using system packages in your virtualenv. You also really shouldn’t run more than a handful of trusted, well-known programs as root. Run your virtualenvs with –no-site-packages (default in recent versions).

    Aug 26, 2013 at 2:01


  • 3

    Thumbs up for this one, the chosen answer (above) fails if a package can’t be found any more. This script simply continues to the next packages, wonderful.

    – Josh

    Jun 3, 2014 at 12:42

758

To upgrade all local packages, you can install pip-review:

$ pip install pip-review

After that, you can either upgrade the packages interactively:

$ pip-review --local --interactive

Or automatically:

$ pip-review --local --auto

pip-review is a fork of pip-tools. See pip-tools issue mentioned by @knedlsepp. pip-review package works but pip-tools package no longer works.

pip-review works on Windows since version 0.5.

19

  • 2

    @hauzer: It doesn’t support Python 3. Though it might be a bug

    – jfs

    Apr 25, 2014 at 0:27


  • 7

    @mkoistinen It’s a good tool but until it’s merged in PIP it means installing something additional which not everyone may desire to do.

    – Wernight

    Jul 22, 2014 at 8:50

  • 2

    @Daniel: pip-tools no longer works, pip-review (fork of pip-tools) works.

    – jfs

    Oct 12, 2015 at 6:00

  • 7

    pip-review works just fine (at least for Python version 3.5.0)

    Feb 13, 2016 at 12:13

  • 30

    To update all without interactive mode: pip-review --local --auto

    – Dlamini

    May 21, 2018 at 1:07