Upgrade outdated packages when using pip

pip install --upgrade $(pip list --outdated | tail -n +3 | awk '{print $1}')

April 16, 2020docdyhr

Explanation

  • pip install --upgrade ... upgrades the packages specified on the command line
  • $(...) is called Command Substitution in Bash. The command within $(...) is executed and its output replaces the $(...) expression. In this example the command inside will produce a list of package names that we want to upgrade.
  • pip list --outdated prints the list of outdated package names, their versions, and other info in a multi-column format
  • | tail -n +3 prints lines from the input starting from the 3rd line. We do this to skip the first two lines of the output of pip list --outdated because those are headers in the output, and we want only the lines that contain actual package names
  • | awk '{print $1}' prints the first column of the input

For example, the output of pip list --outdated might look like this:

Package                Version Latest Type
---------------------- ------- ------ -----
setuptools             56.0.0  68.1.2 wheel
social-auth-app-django 5.2.0   5.3.0  wheel

By piping this to | tail -n +3 | awk '{print $1}' we have simply:

setuptools
social-auth-app-django

Finally, note that many of the long descriptive flag names of the pip command have shorter aliases, for example this is equivalent to the one-liner above:

pip install -U $(pip list -o | tail -n +3 | awk '{print $1}')

Limitations

pip is package management tool for Python, therefore this one-liner is only applicable in Python projects that use pip as their package manager tool of choice.

If pip is not executable directly, then you may need to use python3 -m pip instead.