When working with different packages and modules in Python programming language, you always need to upgrade packages manually, or also you may need to downgrade any certain package because of comaptibility issue. So in this Python tutorial, we will learn how to upgrade all python packages with pip?
Table Of Contents
What is Pip?
pip stands for Preferred Installer Program which comes pre-installed with Python after Python 2.7.9. The
pip is written in python programming language and also the preferred way of installing Python packages and modules. The
pip gets connected to online public library/repository called the Python Package Index and installs/updates the required package and modules. The
Here are some pip commands you might be familiar with:
pip --version: Used to check the current version of pip
pip install package_name: This command is used to install any Python package or module.
python -m pip install --upgrade pip: Used to upgrade pip to a new version.
With Python3, you can run the following command,
python -m pip install --upgrade pip
See the output below:
Requirement already satisfied: pip in c:\program files\python311\lib\site-packages (22.3) Collecting pip Downloading pip-22.3.1-py3-none-any.whl (2.1 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.1/2.1 MB 3.0 MB/s eta 0:00:00 Installing collected packages: pip Successfully installed pip-22.3.1
python -m pip list [options]: This command lists the packages and modules installed. Below is the Output.
With Python3, you can run the command as,
Latest Python - Video Tutorial
Package Version ---------- ------- pip 22.3.1 setuptools 65.5.0
This also has a options argument in which you can pass many useful commands like :
-o, --outdated: List outdated packages
-u, --uptodate: List uptodate packages
--user: List the installed packages for the user logged in into the computer.
Pip has some many useful commands which depends on the user’s use case and requirements. Visit Official Docs for more.
Upgrade a Python package with pip?
Before learning about How to upgrade all Python packages with pip? Let’s learn how can we upgrade a Python Package with pip. So, using
python -m pip list you can see the list of packages installed with their version names. Below is the output:
Package Version ---------- ------- pip 22.3.1 setuptools 65.5.0
You can see above, we have two packages installed with their version names, so now what we will do is using
pip3 install --upgrade setuptools we will upgrade the setuptools package. For example,
pip3 install --upgrade setuptools
Requirement already satisfied: setuptools in c:\program files\python311\lib\site-packages (65.5.0) Collecting setuptools Downloading setuptools-65.6.0-py3-none-any.whl (1.2 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.2/1.2 MB 3.7 MB/s eta 0:00:00 Installing collected packages: setuptools Successfully installed setuptools-65.6.0
You can see earlier the version was 65.5.0. Now it has been successfully upgraded to version 65.6.0.
How to upgrade all python packages with pip?
So to upgrade all packages with pip we have installed some famous Python packages like: Numpy, Django, MoviePy. To install a python package with specific version we use:
pip install package_name==version. See the installed packages with the version numbers then we will discuss ways to upgrade all python packages with pip.
Package Version ------------------ --------- asgiref 3.5.2 certifi 2022.9.24 charset-normalizer 2.1.1 colorama 0.4.6 decorator 4.4.2 Django 4.1.1 idna 3.4 imageio 2.22.4 imageio-ffmpeg 0.4.7 moviepy 1.0.1 numpy 1.23.4 Pillow 9.3.0 pip 22.3.1 proglog 0.1.10 requests 2.28.1 setuptools 65.6.0 sqlparse 0.4.3 tqdm 4.64.1 tzdata 2022.6 urllib3 1.26.12
Method 1 : Using pip freeze
First method we can use is
pip freeze. The command
pip freeze is another python pip command which shows the list of packages installed in your computer with their version number. Below are some steps that need to be followed in the given manner-
pip freeze > packages.txt: This will create a text file named as packages with all the installed packages with their package version.
- Replace all ‘==’ with ‘>=’ as this will install packages with the version greater than the previous one.
pip install -r packages.txt --upgradeas this will upgrade all the packages whose updates are available for the versions greater than the version which is currently installed.
After upgrading through this method, now type
python -m pip list as this will show the program installed with their versions.
Package Version ------------------ --------- asgiref 3.5.2 certifi 2022.9.24 charset-normalizer 2.1.1 colorama 0.4.6 decorator 4.4.2 Django 4.1.3 idna 3.4 imageio 2.22.4 imageio-ffmpeg 0.4.7 moviepy 1.0.3 numpy 1.23.5 Pillow 9.3.0 pip 22.3.1 proglog 0.1.10 requests 2.28.1 setuptools 65.6.0 sqlparse 0.4.3 tqdm 4.64.1 tzdata 2022.6 urllib3 1.26.12
In the above output you can see versions of some programs has been upgraded, maybe some versions do not have new updates. You can see Django whose previous installed version was 4.1.1 has been upgraded to 4.1.3.
Method 2: Using subprocess module and pkg_resources
Another method we can use to upgrade all python packages with pip is by using subprocess module and pkg_resources along with the pip command.
subprocess module is used to execute system commands. This module comes bundled with python. This module is the mostly used and recommended executing system commands.
pkg_resoures is used for some of its advanced features like parallel installation of multiple versions. This module is used to manage Python package dependencies and access bundled files and resources, including those inside of zipped.
Also before trying out this method i have used
pip install package_name==version to install a older version of the package/module. Like i have downgraded the version of Django to 4.1.1 using
pip install Django==4.1.1. Similarly i have downgraded the version of MoviePy and Numpy to 1.0.1 and 1.23.4 respectively.
Now see the below example code
import pkg_resources from subprocess import call # this will return a list of packages installed. packages = [dist.project_name for dist in pkg_resources.working_set] # call() used to execute a command call("pip install --upgrade " + ' '.join(packages), shell=True) # call() will execute another command which lists all the python packages installed with their version numbers. call("python -m pip list", shell=True)
asgiref 3.5.2 certifi 2022.9.24 charset-normalizer 2.1.1 colorama 0.4.6 decorator 4.4.2 Django 4.1.3 idna 3.4 imageio 2.22.4 imageio-ffmpeg 0.4.7 moviepy 1.0.3 numpy 1.23.5 Pillow 9.3.0 pip 22.3.1 proglog 0.1.10 requests 2.28.1 setuptools 65.6.0 sqlparse 0.4.3 tqdm 4.64.1 tzdata 2022.6 urllib3 1.26.12
In the above code and output you can see we have used two different modules along with pip to upgrade all python packages with pip. You can see the version number of the packages like Django, NumPy and MoviePy has been changed or we can say has been upgraded.
So, in this Python tutorial, how to upgrade all python packages with pip?, we used two methods through which we can upgrade all python packages with pip. Also we learned about pip and some useful commands of pip.
Follow this article line by line and read and write all the code in your IDE in order to have a better understanding of the problem. Also some commands or packages/modules can be version specific. We are using Python Version 3.11.0. Type
python --version in your cmd to check your version of Python Installed. Thanks.
Latest Video Tutorials