pip
package management
Python
software upgrade
installation guide

How to update/upgrade a package using pip?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Upgrading Python packages with pip is straightforward, but safe upgrades require more than one command. You need to understand version constraints, virtual environments, and reproducibility for teams. A reliable process prevents breaking dependencies and makes rollbacks easy.

Core Sections

Basic Upgrade Commands

To upgrade a single package, use --upgrade or -U. You can also pin to a specific version when you need controlled changes.

bash
python -m pip install --upgrade requests
python -m pip install "requests==2.32.3"

Using python -m pip is recommended because it ensures the command targets the interpreter you intend.

Upgrade in a Virtual Environment

Always prefer a virtual environment for project work. System-wide upgrades can affect unrelated projects and tooling.

bash
1python -m venv .venv
2source .venv/bin/activate
3python -m pip install --upgrade pip
4python -m pip install -U fastapi
5python -m pip list

On Windows PowerShell, activation is usually .venv\Scripts\Activate.ps1.

Upgrade from a Requirements File

For application repositories, dependencies are typically managed in requirements.txt or a lock-style file. Upgrade intentionally, then freeze resolved versions.

bash
python -m pip install -r requirements.txt
python -m pip install -U numpy pandas
python -m pip freeze > requirements.txt

Be careful with direct freeze in large projects. It can capture transitive packages that your team may prefer to manage differently.

Inspect What Will Change

Before and after upgrading, inspect installed versions and dependency graphs.

bash
python -m pip show sqlalchemy
python -m pip list --outdated
python -m pip check

pip check helps detect incompatible dependency combinations after upgrades.

Use Constraints for Safer Team Upgrades

If several services share compatibility requirements, use a constraints file to enforce upper bounds while still allowing security updates.

bash
python -m pip install -r requirements.txt -c constraints.txt

A constraints strategy reduces surprise breakage when upstream libraries release incompatible major versions.

Rollback and Recovery Strategy

Treat upgrades as change events. Run tests immediately, and if needed, rollback to previously known-good versions.

bash
python -m pip install "pydantic==2.8.2"
pytest -q

Store old dependency snapshots in version control so reverting does not rely on memory.

Keep pip Itself Updated Carefully

Upgrading pip can improve resolver behavior and security, but in CI you may want a pinned pip version for reproducibility.

bash
python -m pip install --upgrade pip
python -m pip --version

If your base image is shared across teams, coordinate resolver version changes as part of infrastructure updates.

Upgrade Planning for Production Services

For production systems, treat dependency upgrades as staged rollouts. Start with security-critical patches, run integration tests, and release to a small environment slice before full rollout. If your service has strict uptime requirements, keep a quick rollback procedure ready with known-good version pins.

bash
1python -m pip install "uvicorn==0.30.6"
2python -m pip install "starlette==0.38.2"
3python -m pip check
4pytest -q

In CI, publish resolved package versions as a build artifact. This gives operators a concrete dependency snapshot tied to each deployment, which helps incident response when behavior changes after upgrades.

For security response workflows, automate pip list --outdated reports on a schedule and review high-risk libraries first. This keeps upgrade work incremental instead of disruptive.

Common Pitfalls

  • Upgrading globally and unintentionally breaking other Python projects.
  • Mixing interpreters and running pip against a different Python version than expected.
  • Upgrading many packages at once without tests, making failures hard to isolate.
  • Ignoring dependency conflict warnings after upgrade.
  • Forgetting to commit updated dependency files after a successful change.

Summary

  • Use python -m pip install -U package for controlled package upgrades.
  • Perform upgrades inside virtual environments whenever possible.
  • Inspect outdated and conflicting packages before and after changes.
  • Use constraints and tests for safer team workflows.
  • Keep rollback paths simple by tracking dependency versions in source control.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.