How to Upgrade Python Packages with Pip

When was the last time you updated Python packages installed via pip? Most users tend to forget that these packages need to be updated as well, since just updating the system repository will not work here.

So let’s take a moment and see how to update old Python packages with pip.

How to use pip to update python packages

Pip (Pip Installs Packages) is a command line utility for managing Python packages. You can think of it like we use apt to manage packages in Ubuntu and Debian.

So let’s dive into how you can use this fabulous utility to manage everything related to Python packages.

1. List obsolete packages

Listing the obsolete packages is the best idea for planning how you plan to update packages, as not many want to update their entire package library at once and want to be selective.

To list deprecated packages from Python, you just need to pair pip command with list possibility and --outdated Flag as pictured:

pip list --outdated
outdated packages

2. Update a specific package

As mentioned before, once you get the list of packages that need to be updated, you can be selective and to update a specific package you have to follow the given command syntax:

pip install package_name -U

For example I want to update the package named anime-api to the latest version, so I use the given command:

pip install anime-api -U
Update Anime API

3. Update the package to a specific version

There is no need to only use the latest version of the software (cough Debian, cough), and if you need to use packages for a specific version, which may or may not be the latest software, you can do so with the given command syntax:

pip install --upgrade <package>==<version>

So I want to update said package xdg to version 5.1, which is one point behind the latest build, so my command would be:

pip install --upgrade xdg==5.1
Upgrading xdg to a specific iteration

4. Update each package with pip

NOTE: I wouldn’t recommend updating each package at once as most of the time the dependencies are too complex to handle.

Read  How to Get Rid of Mice for Good in 14 Steps

To update each python package you need to follow the given command:

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

The above command uses xargs. First it gets the packages that need to be updated and then executes them pip3 install -U command over each packet.

And I used pip3 instead of pip here. Both pip and pip3 commands are available in Ubuntu 22.04 and later.

Wrap up

Updating everything at once has never been a good idea in the case of pip. And I was in a state of broken dependencies so make sure you know what you’re going to have.

And if you have any questions, feel free to ask them in the comments.

Leave a Comment

Your email address will not be published. Required fields are marked *