How to Update a Python Package

Lakshay Kapoor Feb 02, 2024
  1. Use pip to Update a Python Package
  2. Use the Jupyter Notebook to Update a Python Package
  3. Use a Virtual Environment to Update Python Packages
  4. Use pipenv Environment to Update Python Packages
How to Update a Python Package

Every software built in this world needs a routine check from time to time to fix bugs and make the most of that software. Just like every software, Python software also needs to be fixed and updated from time to time. In Python, there are a lot of built-in packages and modules that need updating now and then. It is because a lot of features and built-in functions keeps on adding in them. Another reason is that packages that are way too much outdated may not support the latest version of Python.

This tutorial will demonstrate some methods to update a package in Python.

Use pip to Update a Python Package

To update python packages through the command line of the computer, run the following commands.

pip install --upgrade [package name]==[package version or latest package version]

There might be some cases where the user might have to use pip3 instead of pip.

One more way of updating a Python package in Linux is through the following command.

sudo pip install [package_name] --upgrade

Again, pip or pip3 might vary from one operating system to another.

Using sudo is a more secure way to operate in the command-line as it asks the system password before performing the action.

Use the Jupyter Notebook to Update a Python Package

Jupyter notebook is a free-of-cost, open-source web application that allows a user to code in Python and performs tasks like data visualization, machine learning, etc. It comes under a graphic user interface (GUI) called the Anaconda Navigator that helps launch such web applications and manage its packages and modules simultaneously.

Jupyter notebook can also be used to update Python packages. Following is the command to carry out this task:

!pip install <package_name> --upgrade

Use a Virtual Environment to Update Python Packages

This method is used to update those packages that do not require a specific version of Python in a virtual environment. The python code used in this method uses the pip command.

The pkg_resources module is used in this method. This module helps in finding and manipulating Python packages and versions.

Another module called subprocess is used in this method that helps a user to get and run new programs by getting new processes. This module is generally used for replacing old versions of modules.

import pkg_resources
from subprocess import call

for p in pkg_resources.working_set:
    call("python -m pip install --upgrade " + p.<projectname>, shell=True)

Use pipenv Environment to Update Python Packages

This is one of the easiest ways to update packages that do not require a specific Python version in a specific virtual environment.

pipenv tool helps a user make a virtual environment of its own python project by providing all the necessary means.

Following are the steps to update python packages using pipenv :

Activate the shell that comprises all the packages to be updated

pipenv shell

Now Update all packages:

pipenv update
Lakshay Kapoor avatar Lakshay Kapoor avatar

Lakshay Kapoor is a final year B.Tech Computer Science student at Amity University Noida. He is familiar with programming languages and their real-world applications (Python/R/C++). Deeply interested in the area of Data Sciences and Machine Learning.

LinkedIn