How to upgrade 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.
Читайте также:  Получить номер строки python

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.

Источник

Pip Upgrade – And How to Update Pip and Python

Kolade Chris

Kolade Chris

Pip Upgrade – And How to Update Pip and Python

Python is a widely used and powerful programming language that’s relatively simple to learn.

Python releases patch updates every few months and major updates around once in a year. Because of this, it is always a good idea to update the version of Python you have on your computer.

In addition, you need to update Python so you can get access to the exciting features they add after major updates. For example, there’s quite a speed improvement in Python 3.11 over 3.10.

There’s also a Python package manager called Pip you might need to update occasionally. It is to Python what NPM is to JavaScript.

Starting from Python 3.4, Pip comes with the standard Python distribution. But if you don’t get it after installing Python for some reason, then you need to install it manually.

In this article, I will show you how to update Python on your Mac and Windows computer. I will also show you how to update Pip on the two operating systems.

What We’ll Cover

How to Update Python and Pip on Mac OS

One of the easiest ways to update Python and Pip on Mac is by downloading the package from the Python official website.

When you update Python, the Pip version that comes with it is also updated.

First, check the versions of Python and Pip you have by running python3 —version and pip3 —version :

Screenshot-2023-03-14-at-11.57.29

Screenshot-2023-03-14-at-12.16.47

For me, I picked 3.11 because it’s now stable.

Scroll down and download it for your OS – be it Windows or Mac. I chose Mac becuase I use Mac:

Screenshot-2023-03-14-at-12.18.09

Run the installer and follow every prompt you see.

Screenshot-2023-03-14-at-12.19.43

Confirm the installation by running python3 —version and pip3 —version :

Screenshot-2023-03-14-at-12.21.47

How to Update Python and Pip with Homebrew

If you use Mac, you can also update Python and Pip with Homebrew.

Install pyenv by running brew install pyenv . pyenv is a Python version management tool. It is to Python what NVM (Node version manager) is to JavaScript.

Screenshot-2023-03-14-at-13.29.50

Install any version of Python you want, for instance, 3.9 or 2.7:

Screenshot-2023-03-14-at-13.49.26

You can also update Python by running pyenv latest-version-number . For example, python 3.11 . When you install that Python version, you install Pip too.

How to Update Only Pip with the Terminal

In cases when you want to update only Pip, open your terminal and run pip3 install —upgrade pip . You can then confirm the update by running pip3 —version :

Screenshot-2023-03-14-at-13.02.02

Conclusion

This article took you through how to update Python and Pip by downloading the installation package and using the command line. We also looked at how you can update Pip only if you want to.

If you are using Windows and you want to update Python and Pip, you can also download the latest installer and let the installation wizard guide you through installing it.

Kolade Chris

Kolade Chris

Web developer and technical writer focusing on frontend technologies. I also dabble in a lot of other technologies.

If you read this far, tweet to the author to show them you care. Tweet a thanks

Learn to code for free. freeCodeCamp’s open source curriculum has helped more than 40,000 people get jobs as developers. Get started

freeCodeCamp is a donor-supported tax-exempt 501(c)(3) charity organization (United States Federal Tax Identification Number: 82-0779546)

Our mission: to help people learn to code for free. We accomplish this by creating thousands of videos, articles, and interactive coding lessons — all freely available to the public. We also have thousands of freeCodeCamp study groups around the world.

Donations to freeCodeCamp go toward our education initiatives, and help pay for servers, services, and staff.

Источник

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