Python
package management
update package
pip
software development

How do I update a Python package?

Interview Questions practice on Codemia

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

Browse interview questions

Introduction

Updating a Python package is usually a one-line command, but the real problem is making sure you update the package in the same environment your project actually uses. If that part is wrong, the upgrade can succeed and your code can still import the old version. The safest workflow starts by identifying the exact interpreter and environment first.

Use the Interpreter-Scoped Upgrade Command

The standard upgrade command is:

bash
python -m pip install --upgrade requests

Using python -m pip is safer than typing only pip because it ties the installation to a specific Python executable. That matters on machines with:

  • multiple Python versions
  • virtual environments
  • system and user installs
  • notebook kernels separate from the shell

If your system uses python3, use that explicitly:

bash
python3 -m pip install --upgrade requests

Check the Current Package Version First

Before upgrading, confirm what is installed and where:

bash
python -m pip show requests
python -m pip list --outdated
python -c "import sys; print(sys.executable)"

These commands tell you three useful things:

  • whether the package is installed at all
  • whether a newer version exists
  • which interpreter is about to receive the upgrade

That last check prevents a large number of "pip upgraded it but my app still uses the old version" problems.

Upgrade Inside the Correct Environment

For project work, the package should usually live inside a virtual environment. On macOS or Linux:

bash
source .venv/bin/activate
python -m pip install --upgrade requests

On Windows PowerShell:

powershell
.\.venv\Scripts\Activate.ps1
python -m pip install --upgrade requests

After activation, python -m pip points to that environment. If you skip activation, you may update a global interpreter instead of the one the project is using.

Upgrade to a Specific Version or Range

Sometimes you do not want the newest release. A project may need a known compatible version instead.

bash
python -m pip install requests==2.32.3

Or a version range:

bash
python -m pip install "requests>=2.31,<3"

This is common in production systems where reproducibility matters more than blindly taking the latest release.

Recording the Update in the Project

Upgrading the package in your environment is only part of the job. If the project tracks dependencies in a file, update that record intentionally.

For a requirements.txt based project, a simple flow is:

bash
python -m pip install --upgrade requests
python -m pip show requests

Then update the pinned version in the dependency file if your team expects reproducible installs.

In a Poetry project, the workflow is different because Poetry manages both resolution and the lock file:

bash
poetry update requests

The command depends on the package manager. The principle stays the same: update in the correct environment and then record the resolved version in the project metadata.

User Installs and Permissions

If a system Python blocks writes, pip may need a user install:

bash
python -m pip install --user --upgrade requests

This avoids administrator privileges, but it is not usually the best approach for active project development. Mixing user-level packages with project-specific environments often creates confusion later.

A virtual environment is usually cleaner and more predictable.

Conda Environments

If the package was installed through conda, prefer using conda inside that environment:

bash
conda activate myenv
conda update requests

Mixing pip and conda can work, but it should be done deliberately. If conda owns most of the environment, updating with conda first is usually safer.

Common Pitfalls

The biggest mistake is upgrading the wrong interpreter. A package can update successfully in one Python installation while your project runs from another.

Another mistake is assuming --upgrade means "latest no matter what." Dependency constraints still matter, and pip may keep or resolve versions based on compatibility.

People also forget to update dependency files after changing the environment. That makes the local machine work while the rest of the team still installs older versions.

Finally, do not mix package managers carelessly. If a conda environment owns the dependency graph, prefer conda for upgrades unless you have a clear reason not to.

Summary

  • Use python -m pip install --upgrade package_name so the target interpreter is explicit.
  • Activate the correct virtual or conda environment before upgrading.
  • Check installed and outdated versions before changing dependencies.
  • Pin or constrain versions when reproducibility matters.
  • Treat dependency updates as project changes, not just local machine changes.

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.