Run python -m pip install --upgrade <package-name> (or -U <package-name>) in your terminal with your target virtual environment activated—using python -m pip so the package upgrades inside the exact Python interpreter you intend to run.
Basic Example
# 1. Recommended: Upgrade a single package using python -m pip
python -m pip install --upgrade requests
# 2. Short flag (-U) and upgrading multiple packages in one command
python -m pip install -U numpy pandas matplotlib
# 3. Upgrade pip itself first (recommended before upgrading modern packages)
python -m pip install --upgrade pip
# 4. Verify the newly installed version
python -m pip show requests
Key Details & Edge Cases
- Why
python -m pipbeats barepip install --upgrade:- If multiple Python versions or virtual environments exist on your system, bare
pipcan point to a different Python installation thanpython. Runningpython -m pipguarantees the upgrade modifies thesite-packagesdirectory belonging to that exactpythonexecutable. - On Windows, upgrading
pipvia barepip install --upgrade pipfails withPermissionErrorbecause Windows locks the activepip.exebinary while it runs;python -m pip install --upgrade pipavoids this lock cleanly.
- If multiple Python versions or virtual environments exist on your system, bare
- Dependency upgrade behavior (
--upgrade-strategy):- By default (
--upgrade-strategy only-if-needed),pip install --upgrade pkgupgradespkgto its latest version, and upgrades its dependencies only ifpkg’s new version requires newer dependency versions. - Pass
--upgrade-strategy eager(python -m pip install --upgrade --upgrade-strategy eager pkg) if you also wantpipto upgrade every transitive dependency ofpkgto its latest compatible release.
- By default (
- Including pre-releases (
--pre) and bypassing local cache (--no-cache-dir):- By default,
pipignores alpha, beta, and release-candidate versions (1.0.0rc1) unless no stable release exists. Pass--pre(python -m pip install --upgrade --pre pkg) to upgrade to the latest pre-release. - If a corrupted wheel or stale index metadata blocks an upgrade, pass
--no-cache-dir(python -m pip install --upgrade --no-cache-dir pkg).
- By default,
- Linux system Python (
error: externally-managed-environment/ PEP 668):- On modern Debian/Ubuntu/Fedora systems, running
pip install --upgradeoutside a virtual environment triggers anexternally-managed-environmenterror to protect OS Python packages. Activate your project’s virtual environment (source .venv/bin/activate) first, or usepipx upgrade <cli-tool>for standalone Python CLI tools.
- On modern Debian/Ubuntu/Fedora systems, running
- Upgrading from a
requirements.txtfile: Runpython -m pip install --upgrade -r requirements.txtto upgrade every unpinned (or>=bounded) package listed in your requirements file.
Comparison of pip Upgrade Options & Flags
| Command / Flag | Syntax | What Gets Upgraded | Best Used When |
|---|---|---|---|
| Standard Single Upgrade | python -m pip install -U pkg |
Target package + dependencies if required | Upgrading an existing library to its latest stable PyPI release. |
| Eager Dependency Upgrade | python -m pip install -U --upgrade-strategy eager pkg |
Target package + all compatible dependencies | Refreshing an entire dependency tree to the newest allowed versions. |
| Include Pre-Releases | python -m pip install -U --pre pkg |
Latest release including alpha/beta/RC tags | Testing upcoming release candidates or nightly builds. |
| Upgrade From Requirements | python -m pip install -U -r requirements.txt |
All unpinned/>= packages in file |
Updating project dependencies defined with lower-bound constraints. |
| User-Site Upgrade | python -m pip install -U --user pkg |
Package in user directory (~/.local) |
Upgrading user-installed packages without root/admin privileges (outside a venv). |
�
You must log in or register to comment.
