Sub topic 7
Listing, Inspecting, and Upgrading Packages
This chapter assumes you can create and activate a virtual environment and install packages with python -m pip. Once an environment has a few packages in it, you need to see what is there, check that it is healthy, and update it safely.
Listing what is installed
pip list prints every package in the active environment with its version.
python -m pip listA fresh environment holds only pip (and sometimes setuptools). After you install requests, the list also shows its dependencies, such as urllib3, idna, and certifi. You asked for one package, but pip installed several.
Two options are useful in practice:
--outdatedlists only packages that have a newer release available.--format=jsonprints machine-readable output for scripts.
python -m pip list --outdatedThe output has columns for the installed version, the latest version, and the file type (wheel or sdist).
Inspecting one package
pip show prints metadata for a single package: its version, summary, home page, license, and install location.
python -m pip show requestsTwo fields matter most when you debug:
Locationis thesite-packagesdirectory the package lives in. If it points outside your.venv, you are using the wrong interpreter.RequiresandRequired-byshow the dependency links in both directions. Before you uninstall a package, checkRequired-byto see what else depends on it.
Checking environment health
pip check reports installed packages whose dependencies are missing or incompatible.
python -m pip checkIf everything is consistent, it prints No broken requirements found. Otherwise it names each problem. This can happen when you install two packages that need different versions of a shared dependency. pip's resolver does not stop you from forcing such a state. For example, you can install a package with --no-deps. Run pip check after large installs and in automated builds.
Upgrading packages
Use --upgrade (or -U) to move a package to the newest release that your version specifiers allow.
python -m pip install --upgrade pip
python -m pip install --upgrade requestsUpgrading pip itself is safe inside a virtual environment. It changes only that environment and leaves the system Python alone. Always call it as python -m pip. On Windows, a plain pip install --upgrade pip can fail because the running pip.exe cannot replace itself.
An upgrade respects the specifier you give it. If you run pip install --upgrade "requests<3", pip will not move to a 3.x release.
The following diagram shows a typical maintenance cycle.
Common errors
- Upgrading everything at once. There is no built-in
pip upgrade --all. Scripts that loop over--outdatedcan break a working environment because several major versions change together. Upgrade one package at a time and test after each. - Reading
pip listfrom the wrong interpreter. If the list looks unfamiliar, runpython -c "import sys; print(sys.prefix)"to confirm which environment you are in. - Ignoring
pip check. A clean install message does not mean the dependency set is consistent. - Forgetting to record the result. An upgrade changes the environment but not your
requirements.txt. Update the file when you decide to keep the new versions.
Exercise
Exercise
You run python -m pip show requests and the Location field points to /usr/lib/python3/dist-packages, not your project's .venv. What does this suggest, and how can you confirm it?
Show answer
The command ran under a Python interpreter outside the virtual environment, probably because the environment is not activated. Confirm it by running python -c "import sys; print(sys.prefix)". If the path is not your .venv, activate the environment and run the command again.
Exercise
Which command reports installed packages whose dependencies are missing or incompatible, and when would you run it?
Show answer
python -m pip check. Run it after installing or upgrading several packages and in automated builds, because pip can leave an inconsistent set of packages in place without raising an error.