Explore Courses Blog Tutorials Interview Questions
+4 votes
in Python by (19k 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 (106k 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:-

Learn python with the help of this python training and also visit the python interview questions.

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

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


--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 (7.3k 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 (47.2k points)
You must specify --upgrade in addition to --force-reinstall, else it won't have any effect
0 votes
by (108k 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 (29.5k 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 (47.6k 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.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Oct 3, 2019 in Python by Sammy (47.6k points)
+3 votes
2 answers

Browse Categories