Run python -m pip install --force-reinstall --no-deps <package>==<version> (or omit --no-deps if you also want all transitive dependencies reinstalled) to overwrite corrupted files, missing extensions, or broken metadata for a package version already present in your environment—adding --no-cache-dir when you suspect the downloaded wheel in pip’s local cache is corrupted.
Basic Example
# 1. Recommended: Reinstall ONLY the target package at its exact current version
# (--no-deps prevents pip from needlessly reinstalling every dependency)
python -m pip install --force-reinstall --no-deps requests==2.32.3
# 2. Reinstall the package AND bypass pip's local wheel cache (fixes corrupted downloads):
python -m pip install --force-reinstall --no-deps --no-cache-dir numpy
# 3. Reinstall a package together with ALL of its dependencies:
python -m pip install --force-reinstall requests
Key Details & Edge Cases
- Always pair
--force-reinstallwith--no-depswhen fixing a single broken package:- By default,
pip install --force-reinstall pkguninstalls and reinstallspkgand every transitive dependency in its tree—often unnecessarily rebuilding or re-downloading dozens of healthy packages (likenumpy,typing-extensions, orurllib3). Passing--no-depsrestricts the reinstall strictly to the named package.
- By default,
- What happens if you omit
==<version>(pip install --force-reinstall pkg)?- Without a version pin (
==2.32.3),pipwill fetch the latest compatible version on PyPI rather than keeping your currently installed version. Check your current version first withpython -m pip show <package>and pinpkg==<current_version>if you want the exact same release restored.
- Without a version pin (
- Avoid
--ignore-installed(-I) unless you specifically want to shadow system/base packages:--ignore-installedinstalls fresh files without uninstalling the previous version first, which can leave orphaned.dist-metadata directories or stale.pymodules behind insite-packages.--force-reinstallproperly uninstalls the existing distribution before reinstalling it.
- Always invoke via
python -m pipinstead of barepip:- Running
python -m pip install --force-reinstall ...guarantees you are reinstalling into the exact Python interpreter or active virtual environment (which python/where python), avoiding PATH mismatches across multiple Python installations.
- Running
- C-extension / editable local packages (
pip install -e . --force-reinstall --no-deps):- When developing a local package with C/Cython/Rust extensions,
pip install -e . --force-reinstall --no-depsforces build backends to recompile native binaries and refresh entry-point console scripts.
- When developing a local package with C/Cython/Rust extensions,
Comparison of pip Reinstallation Flags
| Command / Flag Combination | Uninstalls Existing Version First? | Reinstalls Dependencies? | Bypasses Local Wheel Cache? | Best Used When |
|---|---|---|---|---|
python -m pip install --force-reinstall --no-deps pkg==ver (Recommended) |
Yes | No (target package only) | No (uses cached wheel) | Repairing accidental edits, missing files, or broken entry points in a single package. |
python -m pip install --force-reinstall --no-deps --no-cache-dir pkg==ver |
Yes | No | Yes (re-downloads from PyPI) | Fixing OSError / zip corruption errors caused by a damaged wheel in pip’s cache. |
python -m pip install --force-reinstall pkg |
Yes | Yes (reinstalls full dependency tree) | No | Recovering a virtual environment after binary ABI mismatches across an entire stack. |
python -m pip install --ignore-installed pkg |
No (overwrites files in place) | Yes | No | Installing a package into a --target directory or overriding pre-installed OS packages. |
You must log in or register to comment.
