How to Update All Python Packages?

Posted in /  

How to Update All Python Packages?
vinaykhatri

Vinay Khatri
Last updated on October 6, 2024

    Python is well known for its huge set of libraries and the built-in pip package manager. Python comes with an in-built pip terminal command. pip allows Python developers to install and upgrade Python packages from the command terminal. pip is a terminal command tool and the built-in package manager for Python , and with its help, we can install, uninstall, upgrade and display Python packages easily.

    Using the pip install –upgrade <package> command, we can upgrade a single package at a time. This means, in Python, we can not use the pip command to upgrade all the installed packages at once. However, we can use logic and create a Python program that can upgrade all the installed Python packages for us.

    Note: " pip does not provide any pip upgrade all packages or pip update package commands to upgrade all the packages at once. "

    Here in this Python tutorial, you will learn how to create a Python program to upgrade all the outdated Python packages and libraries. We are creating this program because pip does not support commands to upgrade all the packages at once. The steps we have provided below will work on every operating system.

    How to Update All Python Packages Using pip and a Python Program?

    We know that using the pip -install upgrade <package_name> command, we can upgrade the installed Python package. However, this pip command only allows you to upgrade a single package at a time. What if you wish to update all Python packages at once? To do so, follow the steps given below. The updation of all the Python packages is divided into two steps:

    1. Create the txt file of all the outdated packages.
    2. Write and execute a Python program that will update all the Python packages.

    STEP 1: Create an outdated.txt file containing all the outdated Python packages using the pip command.

    The first step is to create an outdated.txt file that will contain the names of all the Python packages that are outdated. We can use the following command to know which Python packages installed on your system are dated: pip list --outdated The above pip command returns a list of all the installed outdated Python packages. Next, type this command on your terminal or command prompt:

    pip list --outdated > outdated.txt

    The above pip command will create an outdated.txt file on your system, and inside the outdated.txt file, all the outdated packages will be listed.

    STEP 2: Write and execute a Python program that will update all the Python packages.

    The second step to update all Python packages is to write and execute a Python program that will fetch all the packages from the outdated.txt file and update them. The Python program given below will do the work for you.

    #upgrade.py

    from subprocess import call
    with open("outdated.txt",'r') as packages_file:
        #these two readline() for first 2 rows
        packages_file.readline()
        packages_file.readline()
    
        package= True
    
        while package:
            package= packages_file.readline()
            if package.split():
                package_name =package.split()[0]
                print("Update-->",package_name)
                call(f"pip install --upgrade {package_name} " , shell=True)

    Execute the Program

    C:\Users\singh\dev> python upgrade.py

    <Note>: Make sure that the upgrade.py and outdated.txt files are on the same directory, else you need to specify the location for the optdated.txt file in the open() method .

    Verify if all the Packages Have Been Upgraded

    Using the pip list --outdated command you can check whether all the Python packages have been upgraded or not.

    pip list --outdated

    Conclusion

    In the above Python tutorial, you learned how to update all Python packages at once. In Python, we do not have a single pip update command to update all the outdated packages. Nonetheless, you can use the Python code mentioned above to do so. It can be difficult to individually trace all the outdated packages in Python, especially if you have a lot of them.

    Thankfully, we have the pip list --outdated command to save the effort. If you have any queries or suggestions related to this article or the program provided above, please let us know by commenting down below. We will try to solve your query as soon as possible.

    Leave a Comment on this Post

    0 Comments