How to force pip to reinstall a package in Python

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
3 min

banner

# 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.

shell
# ๐Ÿ‘‡๏ธ 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

pip install with force reinstall flag

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.

shell
pip install requests --force-reinstall --upgrade --no-deps

reinstall without dependencies

# 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.

shell
pip install requests --ignore-installed

install with ignore installed flag

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.

shell
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.

shell
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.

shell
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.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev