update all python packages in pip

How to Upgrade All Python Packages with Pip

Python provides many modules & packages to help you perform various tasks in your applications & websites. It is always a best practice to keep these packages up-to-date to avail latest features and security patches. If your python installation has many packages, then it may be tedious to update each package individually. In this article, we will learn how to upgrade all python packages with pip, with a one line command.

How to Upgrade All Python Packages with Pip

Here are the steps to upgrade all python packages with pip. Pip does not allow you to do this directly, so we will need to take help of xargs and grep commands for this purpose.

1. Get list of outdated packages

We will use pip command to get a list of outdated packages.

$ pip list --outdated --format=freeze

If you have an older version of pip installed, you can use the following command instead.

$ pip freeze --local

2. Skip editable package definitions

We pipe the output of above command to grep command to skip packages with editable definitions.

$ pip list --outdated --format=freeze | grep -v '^\-e'

3. Extract package names

Next we pipe the above output to cut command to extract package names.

$ pip list --outdated --format=freeze | grep -v '^\-e' | cut -d = -f 1

4. Update Packages

Finally, we will use xargs command to construct individual ‘pip install’ command for each package. We will use -n1 flag to continue running pip install commands, even if one of them fails. xargs command will loop through the list of outdated packages in the input and create & run separate ‘pip install’ commands for them.

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

If you have an older version of pip, you can modify the above command as shown below.

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

In this article, we have learnt how to update all python packages using pip.

Also read:

How to Create Python Function with Optional Arguments
How to Import Python Module Given Path
How to Automate Backup in Python
How to Check Long Running Processes in Linux
How to Backup & Restore Hard Disk in Linux

Leave a Reply

Your email address will not be published.