ChatGPT解决这个技术问题 Extra ChatGPT

Can I force pip to reinstall the current version?

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?

for those looking to re-install pip it self (if it stopped working for some reason ;) ), the answer can be found in this SO q&a

a
arturomp
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).


Any way to force an overwrite when using --target= flag? none of these worked for me. I get the destination path already exists error.
@mrgloom The using cachedjust means it uses source files that where cached on the last install. To force re-download use the --no-cache-dir flag.
pip install -U, for short. (and the --force-reinstall option is rarely necessary)
Note that this command also reinstalls all dependencies. Add --no-deps to avoid that, as suggested in Finn’s answer below.
This does not work for updating pip itself
F
Finn Årup Nielsen

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.


This also works for offline installs, while the excepted answer doesn't.
This is a better solution for packages with a large number of dependencies that do not need to be reinstalled.
sudo was crucial in my case.
Why we need --upgrade when we use --force-reinstall?
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.
D
Davy

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

And if you want to avoid using the local cache, add the option --no-cache-dir
A
Anis LOUNIS aka AnixPasBesoin
--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

You must specify --upgrade in addition to --force-reinstall, or it won't have any effect.
J
Jorge Cribb

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

python -m pip install --upgrade --force-reinstall pip

m
mrgloom
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"


D
Daniel

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