- Python reinstall pip package
- # How to force pip to reinstall a package in Python
- # Only reinstalling the package without its dependencies
- # Ignoring installed packages and reinstalling them
- # Reinstalling all packages in your requirements.txt file
- # Disabling the cache with —no-cache-dir
- # Upgrade your versions of pip , setuptools and wheel
- # Additional Resources
- How to reinstall a pip package even if it exists
- 2 Answers 2
Python reinstall pip package
Last updated: Feb 23, 2023
Reading time · 3 min
# How to force pip to reinstall a package in Python
Use the —force-reinstall option to force pip to reinstall a package, e.g. pip install requests —force-reinstall .
The —force-reinstall option reinstalls the specified packages and their dependencies, even if the packages are up to date.
Copied!# 👇️ force reinstall a package pip install requests --force-reinstall pip3 install requests --force-reinstall # 👇️ force reinstall a package and upgrade to the latest version pip install requests --force-reinstall --upgrade pip3 install requests --force-reinstall --upgrade # 👇️ force reinstall all packages in your requirements.txt file pip install -r requirements.txt --upgrade --force-reinstall
The —force-reinstall option reinstalls the specified packages even if they are already up-to-date.
The —force-reinstall option basically uninstalls the package (and its dependencies) if it’s already installed and then the command installs the package.
The —upgrade option upgrades the specified packages to the newest available version.
# Only reinstalling the package without its dependencies
If you only want to force pip to reinstall a package without reinstalling its dependencies, use the —no-deps option.
Copied!pip install requests --force-reinstall --upgrade --no-deps
# Ignoring installed packages and reinstalling them
If you need to ignore one or more installed packages and reinstall them, you can also use the —ignore-installed option.
Copied!pip install requests --ignore-installed
The —ignore-installed option ignores the installed packages (and their dependencies) and overwrites them.
This is different than —force-reinstall because —force-reinstall uninstalls the package and its dependencies instead of ignoring them and overwriting them.
In general, it’s better to use the —force-reinstall option because it’s cleaner, doesn’t leave orphaned files and is less likely to break things.
# Reinstalling all packages in your requirements.txt file
If you need to reinstall all packages in your requirements.txt file, use the following command.
Copied!pip install -r requirements.txt --upgrade --force-reinstall pip3 install -r requirements.txt --upgrade --force-reinstall
The -r option is a shorthand for —requirement and installs from the given requirements file.
# Disabling the cache with —no-cache-dir
There is also a —no-cache-dir option that can be used to disable the cache.
Copied!pip install requests --no-cache-dir --force-reinstall --upgrade pip3 install requests --no-cache-dir --force-reinstall --upgrade
Pip’s caching is turned on by default with the intent to save time on duplicate downloads and builds.
Using the —no-cache-dir option disables pip’s cache.
# Upgrade your versions of pip , setuptools and wheel
If you got an error while running any of the commands in the article, try upgrading your versions of pip, setuptools and wheel.
Copied!pip install --upgrade pip setuptools wheel pip3 install --upgrade pip setuptools wheel # 👇️ if you don't have pip set up in PATH python -m pip install --upgrade pip setuptools wheel python3 -m pip install --upgrade pip setuptools wheel py -m pip install --upgrade pip setuptools wheel
If you don’t have pip set up in your system’s PATH environment variable, use the python -m command instead.
# Additional Resources
You can learn more about the related topics by checking out the following tutorials:
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
How to reinstall a pip package even if it exists
I want to run a pip install -r requirements.txt command; I want to run the same command over and over again; The issue is that requirements.txt will include some wheel files which may have the same version but different source code; I want to make sure the package will be reinstalled, i.e. fetched again from my custom pip repo; I am aware of this topic, but the distinction between —ignore-installed and —force-reinstall does not seem very clear to me; I have e.g. somepack==1.1 , I change the source code and I want the .whl to be fetched again from my repo when performing pip install ; Which one should I use? Should I incorporate both? What is their difference? The package may have the same version, e.g. somepack==1.1 or it may have incremental versions at some point. e.g. somepack==1.2 I want it to be always (re)installed; edit: This is the help of pip which does not seem very clear to me at least in the above issue
--force-reinstall Reinstall all packages even if they are already up-to-date. -I, --ignore-installed Ignore the installed packages (reinstalling instead).
2 Answers 2
pip install -r requirements.txt --upgrade --force-reinstall
—force-reinstall will remove the existing packages and then install the current versions.
—ignore-installed will just overwrite the existing with the current version, but will not remove files that were deleted in the update, meaning you may have files hanging out in your library install that aren’t part of the library.
—upgrade (redundant in this case), does force-reinstall for only those packages for which there is a new version.