How to update/upgrade a package using pip?

PythonPip

Python Problem Overview


What is the way to update a package using pip? those do not work:

pip update
pip upgrade

I know this is a simple question but it is needed as it is not so easy to find (pip documentation doesn't pop up and other questions from stack overflow are relevant but are not exactly about that)

Python Solutions


Solution 1 - Python

The way is

pip install <package_name> --upgrade

or in short

pip install <package_name> -U

Using sudo will ask to enter your root password to confirm the action, but although common, is considered unsafe.

If you do not have a root password (if you are not the admin) you should probably work with virtualenv.

You can also use the user flag to install it on this user only.

pip install <package_name> --upgrade --user

Solution 2 - Python

For a non-specific package and a more general solution, you can check out pip-review. A tool that checks what packages could/should be updated.

To install:

$ pip install pip-review

Then run:

$ pip-review --interactive
requests==0.14.0 is available (you have 0.13.2)
Upgrade now? [Y]es, [N]o, [A]ll, [Q]uit y

Solution 3 - Python

use this code in teminal :

python -m pip install --upgrade PAKAGE_NAME #instead of PAKAGE_NAME 

for example i want update pip pakage :

 python -m pip install --upgrade pip

more example :

python -m pip install --upgrade selenium
python -m pip install --upgrade requests
...

Solution 4 - Python

tl;dr script to update all installed packages

If you only want to upgrade one package, refer to @borgr's answer. I often find it necessary, or at least pleasing, to upgrade all my packages at once. Currently, pip doesn't natively support that action, but with sh scripting it is simple enough. You use pip list, awk (or cut and tail), and command substitution. My normal one-liner is:

for i in $(pip list -o | awk 'NR > 2 {print $1}'); do sudo pip install -U $i; done

This will ask for the root password. If you do not have access to that, the --user option of pip or virtualenv may be something to look into.

Solution 5 - Python

import subprocess as sbp
import pip
pkgs = eval(str(sbp.run("pip3 list -o --format=json", shell=True,
                         stdout=sbp.PIPE).stdout, encoding='utf-8'))
for pkg in pkgs:
    sbp.run("pip3 install --upgrade " + pkg['name'], shell=True)

> Save as xx.py
> Then run Python3 xx.py
> Environment: python3.5+ pip10.0+

Solution 6 - Python

While off-topic, one may reach this question wishing to update pip itself (See here).

To upgrade pip for Python3.4+, you must use pip3 as follows:

sudo pip3 install pip --upgrade

This will upgrade pip located at: /usr/local/lib/python3.X/dist-packages

Otherwise, to upgrade pip for Python2.7, you would use pip as follows:

sudo pip install pip --upgrade

This will upgrade pip located at: /usr/local/lib/python2.7/dist-packages

Solution 7 - Python

I use the following line to update all of my outdated packages:

pip list --outdated --format=freeze | awk -F '==' '{print $1}' | xargs -n1 pip install -U

Solution 8 - Python

Execute the below command in your command prompt,

C:\Users\Owner\AppData\Local\Programs\Python\Python310>python -m pip install --upgrade pip

Output will be like below,

Requirement already satisfied: pip in c:\users\owner\appdata\local\programs\python\python310\lib\site-packages (21.2.4)
Collecting pip
  Downloading pip-22.0.3-py3-none-any.whl (2.1 MB)
     |████████████████████████████████| 2.1 MB 3.3 MB/s
Installing collected packages: pip
  Attempting uninstall: pip
    Found existing installation: pip 21.2.4
    Uninstalling pip-21.2.4:
      Successfully uninstalled pip-21.2.4
Successfully installed pip-22.0.3

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionborgrView Question on Stackoverflow
Solution 1 - PythonborgrView Answer on Stackoverflow
Solution 2 - Pythonas - ifView Answer on Stackoverflow
Solution 3 - PythonmamalView Answer on Stackoverflow
Solution 4 - PythonAiden WoodruffView Answer on Stackoverflow
Solution 5 - PythonFofdsfView Answer on Stackoverflow
Solution 6 - PythonNick DView Answer on Stackoverflow
Solution 7 - PythonexplogxView Answer on Stackoverflow
Solution 8 - PythonSriram KrishnaswamyView Answer on Stackoverflow