655

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)

borgr
  • 20,175
  • 6
  • 25
  • 35

10 Answers10

1024

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
borgr
  • 20,175
  • 6
  • 25
  • 35
  • 1
    but what if I need to upgrade Package and its dependencies (f/e Flask), does this way work either? – Alexey Nikonov Aug 16 '21 at 14:50
  • 1
    The default is to update dependencies (only) if needed. So, yes. see more here https://stackoverflow.com/questions/2861183/upgrade-package-without-upgrading-dependencies-using-pip/41471712 – borgr Aug 17 '21 at 08:22
105

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
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
as - if
  • 2,729
  • 1
  • 20
  • 26
  • 26
    While this does not answer the question directly, it might be what some people actually look for when they look for this message. – borgr Jun 28 '19 at 04:29
  • 2
    FYI, `pip-review` needs to be installed first with `pip` – karatedog Apr 10 '21 at 23:46
  • 1
    Does `pip-review` checks all packages, including those not installed by `pip`? – Gathide Dec 22 '22 at 09:44
  • Complete output (6 lines): usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...] or: setup.py --help [cmd1 cmd2 ...] or: setup.py --help-commands or: setup.py cmd --help error: invalid command 'bdist_wheel' ---------------------------------------- ERROR: Failed building wheel for pip-review – makkasi May 23 '23 at 04:08
32

Use this code in terminal:

python -m pip install --upgrade PACKAGE_NAME

For example I want update pip package:

python -m pip install --upgrade pip

More examples:

python -m pip install --upgrade selenium
python -m pip install --upgrade requests
...
informatik01
  • 16,038
  • 10
  • 74
  • 104
mamal
  • 1,791
  • 20
  • 14
15

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.

borgr
  • 20,175
  • 6
  • 25
  • 35
Aiden Woodruff
  • 341
  • 3
  • 9
13
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+

Fofdsf
  • 155
  • 3
  • 9
13

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

On newer versions of pip the above breaks, use this instead:

pip list --outdated --format=json | jq '.[].name' | xargs -n1 pip install -U
explogx
  • 1,159
  • 13
  • 28
  • `pip list --outdated --format=freeze` fails here for (currently latest) `pip` version 23.1.2 with `ERROR: List format 'freeze' can not be used with the --outdated option.`; `pip list --outdated --format=json` works --> might use e.g. `jq` to process output instead of `awk`. – ssc May 26 '23 at 10:26
12

Also, in Jupyter notebook, by running the code below in a code cell, you can update your package:

%pip install <package_name> --upgrade
Sara
  • 419
  • 1
  • 6
  • 14
  • Please use current best practices when posting suggestions such as these. You'll want to suggest using the modern magic command `%pip install` that was added to insure the installation occurs in the environment backing the kernel underlying the notebook. The exclamation point alone doesn't do that and can causes issues/confusion. Hence, the impetus for the addition of the magic command. See [here](https://discourse.jupyter.org/t/why-users-can-install-modules-from-pip-but-not-from-conda/10722/4?u=fomightez) for more about the modern magic command. – Wayne Dec 01 '22 at 19:23
9

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

borgr
  • 20,175
  • 6
  • 25
  • 35
Nick D
  • 167
  • 1
  • 4
-1

If you use requirements.txt, you can use an online tool https://pkgui.com/pip

It edits the requirements.txt with the major versions and allows manual changes via dropdown.

enter image description here

tom
  • 9,550
  • 6
  • 30
  • 49
-2

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
  • Hey, welcome to stack Overflow. First, the long path you supplied is not generic, so will confuse others. Second, note that this does not answer the question (requiring updating a package not pip itself). Last, if you answer an already answered question add new info or edit current answer. – borgr Feb 26 '22 at 08:50