Upgrading python with pip

How to upgrade all python packages with pip?

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?

The 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 pip is similar to the node package manager(npm) of Javascript/node.js. Nowadays we use pip3.

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,

Frequently Asked:

python -m pip install --upgrade pip
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,

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-

  1. Type pip freeze > packages.txt : This will create a text file named as packages with all the installed packages with their package version.
  2. Replace all ‘==’ with ‘>=’ as this will install packages with the version greater than the previous one.
  3. Type pip install -r packages.txt —upgrade as 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.

Summary

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.

Источник

How to Upgrade Python Packages with Pip

When was the last that you updated Python packages installed via Pip? Most of the users tend to forget that those packages also need to be updated, as just updating the system repository is not going to work here.

So let’s take a moment and see how to update old Python packages with Pip.

How to use pip to upgrade Python packages

Pip (Pip Installs Packages) is a command line utility to manage python packages. You can think of this as how we use apt to manage packages in Ubuntu and Debian.

So let’s dive deep into how you can use this fab utility to manage everything related to Python packages.

1. List outdated packages

Listing the outdated packages is the best idea to plan how you want to update packages as not many want to update their entire library of packages at once and wants to be selective.

To list outdated packages of Python, you just have to pair pip command with list option and —outdated flag as shown:

outdated packages

2. Upgrade a specific package

Once you get the list of the packages that need to be updated, you can be selective as I mentioned earlier, and to update a specific package, you’ll need to follow the given command syntax:

pip install package_name -U

For example, I want to upgrade the package named anime-api to the most recent version, so I’ll be using the given command:

update anime api

3. Upgrade package to specific version

It is not necessary to use only the most recent version of the software (cough Debian cough) and if you are in need of using packages to a specific version that may or may not be the most recent software, can be done using the given command syntax:

So I want to update the package named xdg to version 5.1 which is one point release behind the most recent build so my command would be:

pip install --upgrade xdg==5.1

upgrade xdg to specific iteration

4. Upgrade every package using Pip

NOTE: I do not recommend upgrading every package at once as most of the time, the dependencies are too complex to be handled.

To upgrade every python package, you’d need to follow the given command:

pip3 list --outdated --format=freeze | grep -v '^\-e' | cut -d = -f 1 | xargs -n1 pip3 install -U 

upgrade everything

The above command utilizes xargs. First, it will grab the packages that are needed to be updated and then perform pip3 install -U command over each package.

And I used pip3 here instead of pip. In Ubuntu 22.04 and later, both pip and pip3 commands are available.

Wrapping Up

Upgrading everything at once has never been a good idea in the case of pip. And I found myself in a state of broken dependencies so make sure you know what you will have.

And if you have any queries, feel free to ask in the comments.

Источник

Оцените статью