Sub topic 7
Listing, inspecting, and upgrading packages
This chapter assumes you can create and activate a virtual environment, and that you can install packages with python -m pip install. Every command below runs with the environment active, so python means the environment's interpreter. Once a few installs have accumulated, you need ways to answer three questions. What is installed? What does a given package depend on? Is anything out of date? This chapter covers pip list, pip show, pip check, and the upgrade options of pip install.
Listing what is installed
python -m pip list prints every distribution in the active environment with its version.
python -m pip listPackage Version
------------------ ---------
certifi 2024.8.30
charset-normalizer 3.4.0
idna 3.10
pip 24.3.1
requests 2.32.3
urllib3 2.2.3The list includes packages you never asked for. requests pulled in certifi, charset-normalizer, idna, and urllib3. These are transitive dependencies, meaning dependencies of your dependencies.
A freshly created environment contains only pip. Depending on your Python version it may also contain setuptools. If pip list shows dozens of packages in a new project, you are probably running the wrong interpreter. The troubleshooting chapter covers that case.
Useful filters and formats
pip list accepts several options that change what it shows or how it prints.
| Option | Effect | Column | Column |
|---|---|---|---|
| `--outdated` (or `-o`) | Only packages with a newer release available. Adds `Latest` and `Type` columns. | ||
| `--uptodate` (or `-u`) | Only packages already at the latest release. | ||
| `--not-required` | Only packages that no other installed package depends on. | ||
| `--editable` (or `-e`) | Only editable installs. | ||
| `--local` (or `-l`) | Hide packages from a global site-packages when the environment can see one. | ||
| `--format columns\ | freeze\ | json` | Choose the output format. The default is `columns`. |
--not-required is the most useful filter for understanding an environment. Its output approximates the packages you installed directly, because anything that is someone else's dependency is hidden. A package you installed on purpose will also be hidden if another installed package happens to depend on it.
python -m pip list --not-requiredThe json format is meant for scripts. This one-liner prints the names of outdated packages.
python -m pip list --outdated --format=json | python -c "import json, sys; print('\n'.join(p['name'] for p in json.load(sys.stdin)))"list versus freeze
pip freeze and pip list --format=freeze both print name==version lines. They differ in two ways.
pip freezeomitspipandsetuptoolsby default. Add--allto include them.pip freezeshows editable installs as a local path or a VCS reference instead of a plain version.
Use pip list to look at an environment and pip freeze to record one. The next chapter covers the recording step in detail.
Inspecting one package
python -m pip show prints the metadata of one or more installed packages.
python -m pip show requestsName: requests
Version: 2.32.3
Summary: Python HTTP for Humans.
Home-page: https://requests.readthedocs.io
Author: Kenneth Reitz
License: Apache-2.0
Location: /home/sam/project/.venv/lib/python3.12/site-packages
Requires: certifi, charset-normalizer, idna, urllib3
Required-by:Three fields matter most in daily work.
- Location is the
site-packagesdirectory the package was installed into. If it is not inside your environment, you are not using the interpreter you think you are. - Requires lists the package's direct dependencies.
- Required-by lists the installed packages that depend on this one. An empty value means nothing else needs it, so uninstalling it will not break another package.
Check Required-by before you uninstall a package. pip uninstall does not stop you from removing something that other packages need. It also does not remove the package's own dependencies. Those stay behind as orphans, and pip list --not-required helps you spot them.
Two options give more detail:
--fileslists every file the package installed, relative toLocation.--verbose(or-v) adds fields such as classifiers and project URLs.
python -m pip show --files requestsFor a machine-readable dump of the whole environment, python -m pip inspect prints a JSON document with every installed distribution and the environment's markers. You will rarely need it, but it is useful for tooling.
Checking dependency health
Installing packages one at a time, or forcing a version, can leave an environment where one package requires something that is missing or at the wrong version. pip check verifies the installed set.
python -m pip checkIf everything is consistent, it prints No broken requirements found. and exits with status 0. Otherwise it prints one line per problem and exits with status 1.
flask 3.0.3 requires werkzeug>=3.0.0, but you have werkzeug 2.3.8 which is incompatible.Because the exit status is non-zero on failure, you can use pip check in a script or CI step. Note that pip check only inspects what is installed. It does not fix anything and does not look at your requirements file.
Finding out what versions exist
Before upgrading, you may want to see which releases a package has. pip index versions queries the configured package index.
python -m pip index versions requestsThe command prints the available versions, newest first, and marks the installed one. Its documentation labels it experimental, so its output format may change. Do not parse it in scripts. The project page on the index shows the same information.
Upgrading packages
pip install does nothing for a package that is already installed at an acceptable version. To move to a newer release, add --upgrade (or -U).
python -m pip install --upgrade requestsTo move to a specific version, whether newer or older, name it. The specifier rules from the previous chapter apply.
python -m pip install "requests==2.31.0"
python -m pip install "requests>=2.31,<3" --upgradeThe second command upgrades to the newest release that satisfies the range. It will not cross into 3.x.
What happens to dependencies
The --upgrade-strategy option controls how pip treats the dependencies of the package you name.
| Strategy | Behavior |
|---|---|
| `only-if-needed` (default) | Upgrade a dependency only if the new version of your package requires it. |
| `eager` | Upgrade every dependency to its newest allowed version. |
The default is the safer choice. With eager, one upgrade command can change many packages, which makes breakage harder to trace. Use eager when you deliberately want to refresh the whole tree, and test afterward.
Preview before changing
Add --dry-run to see what pip would install without installing it.
python -m pip install --upgrade --dry-run requestsThe output ends with a Would install ... line. This makes a good habit before any upgrade that touches shared dependencies.
Upgrading pip itself
Upgrade pip inside the environment, using the module form.
python -m pip install --upgrade pipOn Windows this form matters. A running pip.exe cannot replace itself, so pip install --upgrade pip can fail there. python -m pip runs through the interpreter, so the replacement succeeds. Each environment has its own copy of pip, so upgrading it in one environment does not affect others.
Upgrading everything is a trap
pip has no built-in "upgrade all" command. Scripts that loop over pip list --outdated and upgrade each package one by one ignore the constraints between packages. Each upgrade can pull a shared dependency to a version the next package rejects. Prefer this workflow:
- Run
pip list --outdatedto see candidates. - Upgrade the packages you chose, one or a few at a time, with
--dry-runfirst. - Run
pip checkand your tests. - Re-record the environment (
pip freeze > requirements.txt) only after the tests pass.
Common mistakes
- Reading
pip listfrom the wrong environment. Runpython -c "import sys; print(sys.prefix)"and compare it with theLocationfield ofpip show. - Treating
Latestassafe to install.pip list --outdatedreports the newest release the index offers. That release may be a new major version that breaks your code. Check the changelog, or constrain the version. - Ignoring
pip check. pip's resolver warns about conflicts at install time, butpip checkfinds problems created by earlier installs and manual changes. - Using bare
pip. Thepipcommand on your PATH may belong to a different interpreter thanpython. Usepython -m pipso both always match.
Exercise
You run python -m pip show urllib3 and the output has Required-by: requests. You then run python -m pip uninstall urllib3. What does pip do, and how would you detect the damage afterward?
Show answer
pip asks for confirmation and uninstalls urllib3. It does not refuse because another package depends on it. Running python -m pip check afterward reports that requests requires urllib3, which is not installed, and exits with status 1. Reinstalling with python -m pip install urllib3 (or reinstalling requests) restores the environment.
Exercise
You want to update requests to the newest 2.x release, change as few other packages as possible, and see the plan before anything is installed. Write the command.
Show answer
python -m pip install --upgrade --dry-run "requests>=2,<3". The range keeps the result in the 2.x series. The default only-if-needed upgrade strategy limits changes to dependencies that the new requests release requires. After checking the Would install line, run the same command without --dry-run.