Update all pip packages in python

In this article, we shall see how to update all the python libraries installed in our system or in a virtual environment.

This can be done in the following two ways.

  1. Using pip-review module
  2. Getting the list of packages installed and updating them using subprocess call

1. Using pip review-review module

First we have to install the pip-review model in order to use this method. Let us install the pip review model first by running the following command on the terminal.

 pip install pip-review

After this package is installed run the following command.

 pip-review --interactive

This may take some time depending on the number of packages that we have. Once this command is executed the list of packages that can be updated will appear one by one.

We can choose the following three options for each packages.

  1. Yes (Y/y) – Will update the current package alone.
  2. No (N/n) – Will skip the current package without updating it.
  3. All (A/a) – Will update all the packages in the queue
  4. Quit (Q/q) – Quit the process

2. Getting the list of packages installed and updating them using subprocess call

In this method we will make use of an inbuilt python module called pkg_resources. We can set the environment to current working environment and get a list of packages installed in the environment.

After that we can run the “pip install –upgrade” command using the subprocess call.

The complete code snippet for this method is given below.

This will upgrade the packages one by one.

Conclusion

Hope this article was helpful.

Happy coding!