How To Update/Upgrade A Python Package with Pip?

Python provides the Pip or Python Package Index which is used to add useful extra modules to the Python. The pip is command is provided to search, install, update, and remove the modules provided by the Python Package Index. One of the most popular use cases for pip command is updating existing and already installed Python packages. In this tutorial, we will examine different cases for update and upgrade Python packages.

List Installed Python Packages with Pip

Before updating any package the best practice is listing installed packages. The pip command provides the list parameter which will list installed packages. Also version information about the installed packages will be provided. pip is used for the Python2 and pip3 is used for the Python3.

$ pip list

$ pip2 list

$ pip3 list

The following output will be provided with the package names and version information.

apturl 0.5.2
atomicwrites 1.1.5
attrs 19.3.0
bcrypt 3.1.7
beautifulsoup4 4.8.2
blinker 1.4
Brlapi 0.7.0
certifi 2019.11.28
chardet 3.0.4
Click 7.0
colorama 0.4.3
command-not-found 0.3
cryptography 2.8
cupshelpers 1.0
cycler 0.10.0
dbus-python 1.2.16
decorator 4.4.2
defer 1.0.6
distro 1.4.0
distro-info 0.23ubuntu1
duplicity 0.8.12.0
entrypoints 0.3
et-xmlfile 1.0.1
fasteners 0.14.1
future 0.18.2
html5lib 1.0.1
httplib2 0.14.0
idna 2.8
importlib-metadata 1.5.0
jdcal 1.0
keyring 18.0.1
kiwisolver 1.0.1
language-selector 0.1
launchpadlib 1.10.13
lazr.restfulclient 0.14.2
lazr.uri 1.0.3
lockfile 0.12.2
louis 3.12.0
lxml 4.5.0
macaroonbakery 1.3.1
Mako 1.1.0
MarkupSafe 1.1.0
matplotlib 3.1.2
monotonic 1.5
more-itertools 4.2.0
netifaces 0.10.4
numexpr 2.7.1
numpy 1.19.1
oauthlib 3.1.0
olefile 0.46
openpyxl 3.0.3
packaging 20.3
pandas 0.25.3
paramiko 2.6.0
pexpect 4.6.0
Pillow 7.0.0
pip 20.0.2
pluggy 0.13.0
protobuf 3.6.1
py 1.8.1
pycairo 1.16.2
pycups 1.9.73
PyGObject 3.36.0
PyJWT 1.7.1
pymacaroons 0.13.0
PyNaCl 1.3.0
pyparsing 2.4.6
pyRFC3339 1.1
pytest 4.6.9
python-apt 2.0.0+ubuntu0.20.4.1
python-dateutil 2.7.3
python-debian 0.1.36ubuntu1
pytz 2019.3
pyxdg 0.26
PyYAML 5.3.1
reportlab 3.5.34
requests 2.22.0
requests-unixsocket 0.2.0
scipy 1.3.3
SecretStorage 2.3.1
setuptools 45.2.0
simplejson 3.16.0
six 1.14.0
soupsieve 1.9.5
ssh-import-id 5.10
systemd-python 234
tables 3.6.1
ubuntu-advantage-tools 20.3
ubuntu-drivers-common 0.0.0
ufw 0.36
unattended-upgrades 0.1
urllib3 1.25.8
usb-creator 0.3.7
wadllib 1.3.3
wcwidth 0.1.8
webencodings 0.5.1
wheel 0.34.2
xkit 0.0.0
xlrd 1.1.0
xlwt 1.3.0
zipp 1.0.0

Check If Specified Python Package Is Installated with Pip

As the list parameter lists installed pip packages with their version information we can also check for the specific package by using an external command like grep. We will provide the package name we want to check and list. In the following example, we will check for different packages. If the package is not installed there will be no output. We can also provide only some part of the package name and all matched packages will be listed below.

$ pip3 list | grep zipp
zipp                   1.0.0               
$ 
$ pip3 list | grep setuptools
setuptools             45.2.0              
$ 
$ pip3 list | grep django
$ 
$ pip3 list | grep Py
PyGObject              3.36.0              
PyJWT                  1.7.1               
PyNaCl                 1.3.0               
PyYAML                 5.3.1          

List Outdated Python Packages

Python is a dynamic programming language and the modules provided by Pip updated regularly. Especially popular packages and projects get more frequent updates. After installing a package it will be outdated and should be updated. The pip command provides the list --outdate parameter which will list only outdated packages. This command will connect to the Pip repositories and collects installed packages information. It may take some time as all installed packages will be queried from the Pip repository.

$ pip3 list --outdated

Package            Version       Latest    Type 
------------------ ------------- --------- -----
atomicwrites       1.1.5         1.4.0     wheel
attrs              19.3.0        20.2.0    wheel
bcrypt             3.1.7         3.2.0     wheel
beautifulsoup4     4.8.2         4.9.3     wheel
certifi            2019.11.28    2020.6.20 wheel
Click              7.0           7.1.2     wheel
colorama           0.4.3         0.4.4     wheel
cryptography       2.8           3.2.1     wheel
distro             1.4.0         1.5.0     wheel
distro-info        0.23ubuntu1   0.10      sdist
duplicity          0.8.12.0      0.8.16    sdist
fasteners          0.14.1        0.15      wheel
html5lib           1.0.1         1.1       wheel
httplib2           0.14.0        0.18.1    wheel
idna               2.8           2.10      wheel
importlib-metadata 1.5.0         2.0.0     wheel
jdcal              1.0           1.4.1     wheel
keyring            18.0.1        21.4.0    wheel
kiwisolver         1.0.1         1.3.0     wheel
lazr.restfulclient 0.14.2        0.14.3    sdist
lazr.uri           1.0.3         1.0.5     sdist
lxml               4.5.0         4.6.1     wheel
Mako               1.1.0         1.1.3     wheel
MarkupSafe         1.1.0         1.1.1     wheel
matplotlib         3.1.2         3.3.2     wheel
more-itertools     4.2.0         8.5.0     wheel
netifaces          0.10.4        0.10.9    sdist
numpy              1.19.1        1.19.2    wheel
openpyxl           3.0.3         3.0.5     wheel
packaging          20.3          20.4      wheel
pandas             0.25.3        1.1.3     wheel
paramiko           2.6.0         2.7.2     wheel
pexpect            4.6.0         4.8.0     wheel
Pillow             7.0.0         8.0.1     wheel
pip                20.0.2        20.2.4    wheel
pluggy             0.13.0        0.13.1    wheel
protobuf           3.6.1         3.13.0    wheel
py                 1.8.1         1.9.0     wheel
pycairo            1.16.2        1.20.0    sdist
pycups             1.9.73        2.0.1     sdist
PyGObject          3.36.0        3.38.0    sdist
PyNaCl             1.3.0         1.4.0     wheel
pyparsing          2.4.6         2.4.7     wheel
pytest             4.6.9         6.1.1     wheel
python-dateutil    2.7.3         2.8.1     wheel
python-debian      0.1.36ubuntu1 0.1.38    wheel
pytz               2019.3        2020.1    wheel
pyxdg              0.26          0.27      wheel
reportlab          3.5.34        3.5.54    wheel
requests           2.22.0        2.24.0    wheel
scipy              1.3.3         1.5.3     wheel
SecretStorage      2.3.1         3.1.2     wheel
setuptools         45.2.0        50.3.2    wheel
simplejson         3.16.0        3.17.2    wheel
six                1.14.0        1.15.0    wheel
soupsieve          1.9.5         2.0.1     wheel
urllib3            1.25.8        1.25.11   wheel
wadllib            1.3.3         1.3.4     sdist
wcwidth            0.1.8         0.2.5     wheel
wheel              0.34.2        0.35.1    wheel
xlrd               1.1.0         1.2.0     wheel
zipp               1.0.0         3.4.0     wheel

From the output we can see that the installed version of the package and most recent version provided by the Pip is listed for every package.

Update/Upgrade Python Package To The Latest Version with Pip

Now we have learned every step before updating or upgrade a Python package with pip. By the way, both terms update and upgrade can be used for an update package but the pip command uses the install –user –upgrade parameter to update the package. In the following example, we will update a specific package named lxml .

$ pip3 install --user --upgrade lxml

Update/Upgrade Python Package To Specific Version with Pip

By default, the specified package will be updated or upgraded to the latest version. But this can be a problem if we need a specific version of the Python package. This generally occurs where some software depends on and supports a specific version of the Python package. We can provide the version we want to update with the == sign and provide the version information.

$ pip3 install --user --upgrade lxml==4.6.1

Downgrade Python Package To The Specific version with Pip

The downgrade is the operation of installing an older version of the specified package. We can downgrade the Python package to the specific version by using the == with the install command.

$ pip3 install --user lxml==4.6.1

Leave a Comment