+4 votes
1 view
in Python by (12.5k points)

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?

5 Answers

+6 votes
by (67.1k points)
edited by

Yes, you can force pip to reinstall the current version below is the command for the same:-

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

When you are upgrading the pip, you should reinstall all packages even if they are already up-to-date, In that case, you should ignore the installed packages (reinstalling instead). You can use the following command to ignore the installed packages.

pip install -I <package>

pip install --ignore-installed <package>

To know more about this you can have a look at the following video:-

by (17.7k points)
This solution worked for me!
by (7.4k points)
Very well explained. Thank you.
by (12.5k points)
Does it also works on distributed environment like Google Colab.
by (32.8k points)
Yes, then it will be like this:
!pip install --upgrade --force-reinstall <package>
by (42k points)
using cached uses the previously stored in the cache, use --no-cache-dir flag to reinstall those too.
by (19.9k points)
Great! It worked.
0 votes
by (23.6k points)

For this question, you need to have all three options: 

--upgrade 

--force-reinstall ensures reinstallation, 

--no-deps avoids reinstalling dependencies.

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

Otherwise, that pip starts to recompile Numpy or other large packages.

by (5.1k points)
This is a more suitable solution for packages having a huge number of dependencies that do not need to be reinstalled. This also serves for offline installs, while the @vishal answer doesn't.
by (36.7k points)
You must specify --upgrade in addition to --force-reinstall, else it won't have any effect
0 votes
by (44.6k points)

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

pip install -r requirements.txt --ignore-installed

0 votes
by (27.7k points)

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

 

+1 vote
by (27.5k points)

It is good to have all three options with you: --upgrade and --force-reinstall ensures reinstallation, while --no-deps avoids reinstalling dependencies.

See the command below:-

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

If you don't follow these steps you might run into the problem that pip starts to recompile Numpy or other large packages.

...