You can select 'a' to upgrade all packages; if one upgrade fails, run it again and it continues at the next one.
pip
?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)
With Python, the best practice of pinning all the packages in an environment at a specific version ensures that the environment can be reproduced months or even years later.
As packages age, many of them are likely to have vulnerabilities and bugs logged against them. In order to maintain the security and performance of your application, you’ll need to update these packages to a newer version that fixes the issue.
The pip package manager can be used to update one or more packages system-wide. However, if your deployment is located in a virtual environment, you should use the Pipenv package manager to update all Python packages.
The easiest way to update all packages in a Windows environment is to use pip in conjunction with Windows PowerShell:
pip freeze | %{$_.split('==')[0]} | %{pip install --upgrade $_}
This will upgrade all packages system-wide to the latest version available in the Python Package Index (PyPI).
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.