812

I've come across situations where a current version of a package seems not to be working and requires reinstallation. But pip install -U won't touch a package that is already up-to-date. I see how to force a reinstallation by first uninstalling (with pip uninstall) and then installing, but is there a way to simply force an "update" to a nominally current version in a single step?

orome
  • 45,163
  • 57
  • 202
  • 418
  • 3
    for those looking to re-install pip it self (if it stopped working for some reason ;) ), the answer can be found in [this](https://stackoverflow.com/questions/58451650/pip-no-longer-working-after-update-error-module-object-is-not-callable) SO q&a – nsof Nov 16 '19 at 17:18

10 Answers10

1207
pip install --upgrade --force-reinstall <package>

When upgrading, reinstall all packages even if they are already up-to-date.

pip install -I <package>
pip install --ignore-installed <package>

Ignore the installed packages (reinstalling instead).

arturomp
  • 28,790
  • 10
  • 43
  • 72
KGo
  • 18,536
  • 11
  • 31
  • 47
  • 2
    Any way to force an overwrite when using --target= flag? none of these worked for me. I get the destination path already exists error. – radtek Aug 05 '14 at 20:09
  • @KeeganQuinn do you think that's what Karan meant by "When upgrading"...? I suppose so. But your clarification certainly helps me. – Nate Anderson Sep 06 '15 at 18:32
  • What if I want to make a change in zipline which is installed in the process of `pip install pipeline-live`, and simply pick up my change in zipline? – gseattle Jan 30 '19 at 06:18
  • 1
    Including `--upgrade` when `--force-reinstall` is being used shouldn't be needed as of pip 10.0, FYI: https://github.com/pypa/pip/issues/1139 – cjerdonek Feb 01 '19 at 16:45
  • While installing it saying `Using cached` for some packages, how to force their reinstall also? – mrgloom Jun 06 '19 at 17:14
  • 3
    @mrgloom The `using cached`just means it uses source files that where cached on the last install. To force re-download use the `--no-cache-dir` flag. – lcnittl Jul 25 '19 at 07:03
  • I don't want to reinstall any dependencies - only just the single package. Is this possible / how ? Ah just see from next answer seems to be `--no-deps` – WestCoastProjects Sep 14 '19 at 12:50
  • 1
    `pip install -U`, for short. (and the `--force-reinstall` option is rarely necessary) – smci Jun 15 '20 at 21:41
  • 4
    Note that this command also reinstalls all dependencies. Add `--no-deps` to avoid that, as suggested in Finn’s answer below. – Skippy le Grand Gourou Jan 11 '21 at 13:03
  • 2
    This does not work for updating pip itself – Hektor Feb 28 '21 at 10:31
  • "reinstall all packages", I don't understand what this does. You need to specify a package name, but it reinstalls every installed package? This is what I want to do, but I don't know which package-name to use. – Annan Yearian Nov 16 '22 at 11:27
290

You might want to have all three options: --upgrade and --force-reinstall ensures reinstallation, while --no-deps avoids reinstalling dependencies.

$ sudo pip install --upgrade --no-deps --force-reinstall <packagename>

Otherwise you might run into the problem that pip starts to recompile Numpy or other large packages.

Finn Årup Nielsen
  • 6,130
  • 1
  • 33
  • 43
  • 2
    This also works for offline installs, while the excepted answer doesn't. – orodbhen Jun 01 '18 at 14:24
  • 6
    This is a better solution for packages with a large number of dependencies that do not need to be reinstalled. – Assil Ksiksi Nov 15 '18 at 15:43
  • 1
    `sudo` was crucial in my case. – mrgloom Aug 19 '19 at 12:12
  • 2
    Why we need `--upgrade` when we use `--force-reinstall`? – mrgloom Aug 19 '19 at 12:13
  • @FinnÅrupNielsen why it should upgrade current version? as I understand here we want to reinstall package. What if `==` format is used? – mrgloom Aug 19 '19 at 17:06
  • @mrgloom `--force-reinstall` alone does not necessarily upgrade to the current version (at least not in pip 9.0.1). There has been an upgrade according to https://github.com/pypa/pip/issues/1139 see cjerdonek's answer, so newer versions do not require the option. – Finn Årup Nielsen Aug 19 '19 at 17:28
  • Why do you have `sudo` here?? – ComputerScientist Nov 14 '19 at 01:48
  • 1
    macOS: You shouldn't run sudo with pip on a mac . Run as admin rights user but without sudo . On Linux (Ubuntu): it makes sense to run with `sudo` to install for all users. Don't run sudo with `--user` as that will install packages under `root` user only. – wesinat0r Jul 21 '20 at 12:46
  • So by default all dependent projects likewise get updated too from the `--upgrade` flag, is that right? – jxramos Dec 18 '20 at 20:04
60

If you want to reinstall packages specified in a requirements.txt file, without upgrading, so just reinstall the specific versions specified in the requirements.txt file:

pip install -r requirements.txt --ignore-installed
Davy
  • 1,720
  • 1
  • 19
  • 42
  • And if you want to avoid using the local cache, add the option --no-cache-dir – Davy Feb 15 '22 at 14:31
  • This may still upgrade packages, though, if the version restrictions in the requirements.txt allow for it. – Alperino Dec 02 '22 at 14:22
  • Indeed, but if you force them to a fixed version with '==' then no upgrades will happen – Davy Dec 03 '22 at 13:52
46
--upgrade --force-reinstall

doesn't appear to force reinstall using python2.7 with pip-1.5

I've had to use

--no-deps --ignore-installed
anemes
  • 727
  • 6
  • 12
21
sudo pip3 install --upgrade --force-reinstall --no-deps --no-cache-dir <package-name>==<package-version>

Some relevant answers:

Difference between pip install options "ignore-installed" and "force-reinstall"

mrgloom
  • 20,061
  • 36
  • 171
  • 301
21

In the case you need to force the reinstallation of pip itself you can do:

python -m pip install --upgrade --force-reinstall pip
Jorge Cribb
  • 396
  • 2
  • 4
9

If you have a text file with loads of packages you need to add the -r flag

pip install --upgrade --no-deps --force-reinstall -r requirements.txt
Daniel
  • 2,028
  • 20
  • 18
1

I had a Jupyter notebook open using the python kernel that has the package loaded already. I closed that notebook and tried again and it worked.

Worthy7
  • 1,455
  • 15
  • 28
0

pip install --force-reinstall

this can be used

-1

If you work on MacOS and are using Homebrew, run:

/opt/homebrew/opt/python@3.11/bin/python3.11 -m pip install --upgrade pip

of course use the appropriate path for your version

RaamEE
  • 3,017
  • 4
  • 33
  • 53