Upgrade all packages in Python using pip
In this article, we will learn to upgrade all Python packages using pip manager. We will use some built-in functions, pip Python manager available in Python to upgrade all packages available in Python. Let's first have a quick look over what is a pip in Python.
The Pip Python Package Manager
Programmers generally use virtual environments and pip package while working with the Python programming language. When working with projects in Python, users have packages versions being used are defined, which starts growing with time and some packages start to be outdated. pip
Python manager is designed to upgrade the python packages system-wide. Let us look at different ways to use pip to upgrade packages from older versions to newer or latest versions.
Update all packages using pip on Windows
This is the easier way to upgrade packages by using pip
in conjunction with Windows PowerShell. Open your command shell and enter the below command. This will upgrade all packages system-wide to the latest or newer version available in the Python Package Index (PyPI)
.
pip freeze | %{$_.split('==')[0]} | %{pip install --upgrade $_}
Update all packages using pip on Linux
Linux provides a number of ways to use pip
in order to upgrade python packages. This includes two ways using grep and awk.
- Use
grep
to upgrade packages - The grep is to skip editable ("-e") package definitions, and the -n1 flag for xargs that prevents stopping everything, if updating one package fails.
pip3 list --outdated --format=freeze | grep -v '^\-e' | cut -d = -f 1 | xargs -n1 pip3 install -U
- Use
awk
to upgrade packages - The below command first lists all outdated packages, then fetches the first column and converts the multiline result from cut
into a single-line, and forms a space-separated list. It then skips header lines, fetches the first column and takes 1 argument from the pipe left of it, and at last passes it to the command to upgrade the list of packages.
pip3 list -o | cut -f1 -d' ' | tr " " "\n" | awk '{if(NR>=3)print)' | cut -d' ' -f1 | xargs -n1 pip3 install -U
Command for either Windows or Linux for updating packages
pip freeze
first outputs a list of installed packages into a requirements file (requirements.txt). Then the user needs to edit requirements.txt, and replace all ‘==
’ with ‘>=
’. Use the ‘Replace All’ command in the editor. It then upgrades all outdated packages.
#outputs the list of installed packages
pip freeze > requirements.txt
#updates all packages
pip install -r requirements.txt --upgrade
Updating all packages in a Virtual Environment
The easiest way to update unpinned packages (i.e., packages that do not require a specific version) in a virtual environment is to run the following Python script that uses pip. Unlike pip freeze
, this command will not print warnings and FIXME errors.
For pip < 10.0.1
import pkg_resources
from subprocess import call
for dist in pkg_resources.working_set:
call("python -m pip install --upgrade " + dist.<projectname>, 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)
Updating all Local Packages using pip-review
This command updates only local Python packages. However, this command may not be feasible because it might sometimes generate errors and pip.review
may or may not support Python 3 version. pip-review
is a fork of pip-tools
. pip-review
package works but pip-tools
package no longer works in the latest versions of Python.
$ pip install pip-review
$ pip-review --local --interactive
Conclusion
In this article, we learned different commands to upgrade or update all Python packages using pip manager in Python. We saw two main methods such as pip freeze
and pip review
to update packages.