How to force pip to reinstall a package (pip install --force-reinstall)
Codemia Knowledge Hub ·
Yes. pip install --force-reinstall <package> uninstalls a package and installs it again, which fixes corrupted installations, missing files and broken compiled extensions. Without a version specifier pip may pick the latest release instead of the one you have, so to reinstall the current version, pin it: pip install --force-reinstall requests==2.31.0.
The basic command
It does three things in order:
- Uninstalls the existing
requestspackage. - Fetches the package from PyPI (or your configured index), or from pip's local cache if a cached wheel exists.
- Installs it fresh.
Check what you have first so you know which version to pin:
Output:
Reinstall flags compared
| Flag | What it does | Re-downloads? | Reinstalls deps? | Use when |
--force-reinstall | Uninstalls and reinstalls the package and all its dependencies | Yes | Yes | Corrupted install, broken C extensions |
--force-reinstall --no-deps | Reinstalls only the target package, leaves dependencies alone | Yes | No | Only the target package is broken |
--ignore-installed | Installs on top of existing files without uninstalling first | Yes | No | Quick overwrite, virtual env issues |
--no-cache-dir --force-reinstall | Force reinstall without using the local cache | Yes (from network) | Yes | Cached wheel is corrupted |
Reinstall a specific version
Reinstall without touching dependencies
This is the most common real-world form. By default --force-reinstall also reinstalls every dependency, which can take minutes and break working packages with pinned versions. If only numpy is broken, there is no reason to reinstall anything else.
Reinstall and bypass the cache
pip stores wheels in a cache directory and reuses them. If the problem is a corrupted download, reinstalling from cache puts back the same broken file, so --no-cache-dir forces a fresh download.
Reinstall from a requirements file
This reinstalls every package in the file, which is useful when setting up a fresh virtual environment from a known-good requirements file.
Typical cases where a reinstall fixes things
Corrupted package after an interrupted install
C extension built for a different Python version
This happens when you upgrade Python (e.g., 3.11 to 3.12) but reuse the same virtual environment:
Compiled packages broken by a system update
After an OS update or libc upgrade, compiled packages may break:
Reinstalling from a local wheel
Reinstalling an editable (development) install
This refreshes entry points and package metadata while keeping editable mode. Leave out -e and pip converts the editable install into a regular one, which breaks your development workflow.
Finding broken dependencies first
pip check reports dependency conflicts. If it finds any, a targeted --force-reinstall of the conflicting package often resolves them.
--force-reinstall vs uninstall then install
| Approach | Atomic? | Handles compiled cache | Reinstalls deps |
--force-reinstall | Yes (single command) | Uses cache unless --no-cache-dir | Yes (unless --no-deps) |
uninstall + install | No (two commands) | Uses cache | No |
--force-reinstall is a single transaction: if something fails midway, pip rolls back. With two separate commands you can end up with the package uninstalled and the reinstall failed.
Recreating the virtual environment
Work inside a virtual environment. If pip install --force-reinstall outside one asks for sudo on Linux or macOS, that is a sign you should be using one. Never force-reinstall packages in the macOS system Python (/usr/bin/python3); use a virtual environment or Homebrew Python.
When things are badly broken, delete the environment and rebuild it:
This guarantees a clean state. For a single broken package, --force-reinstall --no-deps is faster.
Related reading
- Can I get JSON to load into an OrderedDict?
- Can I make this function more efficient Project Euler Number 9?
- Can I send callbacks to a KerasClassifier?
- Can I serve multiple clients using just Flask app.run as standalone?
- Can I set max_retries for requests.request?
- Can I use bigchainDB server with django instead of using sqlite?
- Can I use boto3 anonymously?
- Can list_display in a Django ModelAdmin display attributes of ForeignKey fields?
System design interview problems on this topic
- Design a Web Crawler: solution and walkthrough
- Design a Task Scheduler: solution and walkthrough
- Design a URL Shortener (TinyURL): solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.