How to upgrade all Python packages with pip?
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Forest of Spells Looping
--
Chapters
00:00 Question
00:24 Accepted answer (Score 2772)
01:40 Answer 2 (Score 814)
02:14 Answer 3 (Score 802)
02:58 Answer 4 (Score 444)
04:13 Thank you
--
Full question
https://stackoverflow.com/questions/2720...
Question links:
[pip]: https://pypi.python.org/pypi/pip
[a feature request]: https://github.com/pypa/pip/issues/4551
Accepted answer links:
[mutually exclusive]: https://pip.pypa.io/en/stable/news/#v22-...
[@andsens]: https://stackoverflow.com/users/339505/a...
Answer 3 links:
[pip-review]: https://github.com/jgonggrijp/pip-review
[pip-tools]: https://github.com/nvie/pip-tools/issues...
[@knedlsepp]: https://stackoverflow.com/questions/2720...
[pip-review]: https://github.com/jgonggrijp/pip-review...
[since version 0.5]: https://stackoverflow.com/questions/2720...
Answer 4 links:
[NumPy]: https://en.wikipedia.org/wiki/NumPy
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #pip
#avk47
ACCEPTED ANSWER
Score 2898
There isn't a built-in flag yet. Starting with pip version 22.3, the --outdated and --format=freeze have become mutually exclusive. Use Python, to parse the JSON output:
pip --disable-pip-version-check list --outdated --format=json | python -c "import json, sys; print('\n'.join([x['name'] for x in json.load(sys.stdin)]))" | xargs -n1 pip install -U
If you are using pip<22.3 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
grepis to skip editable ("-e") package definitions, as suggested by @jawache. (Yes, you could replacegrep+cutwithsedorawkorperlor...).The
-n1flag forxargsprevents 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!
ANSWER 2
Score 918
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 is looking for a new maintainer.
pip-review works on Windows since version 0.5.
ANSWER 3
Score 840
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)
ANSWER 4
Score 474
The following works on Windows and should be good for others too ($ is whatever directory you're in, in the command prompt. For example, C:/Users/Username).
Do
$ pip freeze > requirements.txt
Open the text file, replace the == with >=, or have sed do it for you:
$ sed -i 's/==/>=/g' requirements.txt
and execute:
$ pip install -r requirements.txt --upgrade
If you have a problem with a certain package stalling the upgrade (NumPy sometimes), just go to the directory ($), comment out the name (add a # before it) and run the upgrade again. You can later uncomment that section back. This is also great for copying Python global environments.
Another way:
I also like the pip-review method:
py2
$ pip install pip-review
$ pip-review --local --interactive
py3
$ pip3 install pip-review
$ py -3 -m pip-review --local --interactive
You can select 'a' to upgrade all packages; if one upgrade fails, run it again and it continues at the next one.