Upgrade python packages from requirements.txt using pip command
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
You can tell pip to install or upgrade everything listed in requirements.txt, but the outcome depends on how those requirements are written. If the file pins exact versions, pip will honor those pins rather than jumping to the newest release. A safe upgrade workflow starts with understanding whether you want to reinstall the declared versions, upgrade within allowed ranges, or deliberately move the dependency file itself to newer versions.
The Basic Command
To install or upgrade the packages declared in a requirements file:
This tells pip to read the file and upgrade packages as needed to satisfy those requirements.
Important nuance:
- if the file says
requests==2.31.0,pipwill install exactly2.31.0 - if the file says
requests>=2.31.0,pipmay install a newer compatible release - if the file says just
requests,pipwill usually install the newest available version
So the command upgrades relative to the requirement specification, not relative to "latest everything no matter what."
See What the File Actually Means
A file like this is pinned:
Running pip install --upgrade -r requirements.txt here will not move those packages beyond the pinned versions.
A file like this is range-based:
Now pip has room to pick newer releases.
That distinction explains why many people think --upgrade "does nothing" when the real issue is that the file pins exact versions.
Upgrade Installed Packages and Rewrite the File
If you actually want to move the dependency file forward, one common workflow is:
- upgrade packages in the environment
- run tests
- freeze the new environment back into a requirements file
Example:
This is simple, but it can also capture many transitive dependencies that you may not want to manage manually. For larger projects, tools like pip-tools are often better for curated upgrades.
Use a Virtual Environment
Always upgrade in a virtual environment unless you have a very specific system-level reason not to.
This keeps the upgrade isolated and makes rollback easy. It also prevents one project from silently changing another project's dependency set.
Dry-Run Thinking and Safer Review
Before a large upgrade, inspect what is outdated:
This gives you a better picture of which dependencies are candidates for change. Then you can decide whether to:
- upgrade everything
- upgrade a small set first
- keep some packages pinned for compatibility reasons
Blindly upgrading a large environment and only then discovering a breaking change is usually avoidable.
Requirements Files Versus Modern Dependency Management
requirements.txt is still common, but it is a low-level format. If your project has more than a few dependencies, consider whether you need:
- a primary input file for direct dependencies
- a compiled lock file for exact versions
- a repeatable upgrade workflow
That is why many teams use pip-tools, Poetry, or another dependency manager instead of editing a single flat requirements file forever.
Common Pitfalls
- Expecting
--upgradeto bypass exact version pins inrequirements.txt. - Upgrading global Python packages instead of using a virtual environment.
- Overwriting
requirements.txtwithpip freezewithout reviewing the new dependency graph. - Upgrading everything at once without running tests immediately after.
- Treating direct dependencies and transitive dependencies as if they should always be managed the same way.
Summary
- The core command is
python -m pip install --upgrade -r requirements.txt. - Whether packages actually move depends on the version specifiers inside the file.
- Exact pins prevent automatic upgrades beyond those pinned versions.
- Use a virtual environment and review outdated packages before large upgrades.
- If you want maintainable upgrades over time, consider a workflow that separates direct dependency intent from fully pinned lock output.

