Find which version of package is installed with pip
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
The fastest way to check which version of a Python package is installed is python -m pip show package-name. The Version: line in the output gives you the exact installed version. The critical detail most developers miss is that you must run this against the correct Python interpreter, because different virtual environments, system installs, and conda environments each maintain their own independent package versions.
pip show for a Single Package
This is the primary command for checking one specific package:
Output:
Several fields here are useful beyond just the version:
- Location tells you which environment the package lives in. If this points to a system path rather than your virtualenv, you are checking the wrong Python.
- Requires lists direct dependencies of this package.
- Required-by shows which installed packages depend on this one, which is important before upgrading or removing.
Why python -m pip instead of bare pip
Running python -m pip guarantees that pip executes under the interpreter you specified. A bare pip command might resolve to a different Python installation, especially on systems with Python 2 and Python 3 coexisting or when a virtualenv is not activated.
pip list for Browsing the Environment
When you want to see all installed packages or are not sure of the exact package name:
Output (truncated):
Filtering with grep
Combine with grep to search for a specific package or pattern:
Output formats
pip list supports several formats useful for scripting:
The --outdated flag is particularly useful for maintenance. It shows the installed version alongside the latest available version:
pip freeze for Reproducible Dependency Snapshots
pip freeze outputs installed packages in a format directly usable as a requirements.txt file:
To check a specific package:
The difference between pip list and pip freeze is intent. list is for human browsing. freeze is for generating pinned dependency files. freeze excludes pip itself and other installer packages by default.
Checking the Version From Inside Python
Sometimes you need to verify the version from within a running script or notebook, not from the command line. Python's importlib.metadata module (available since Python 3.8) handles this:
For older Python versions, the pkg_resources module works but is slower and heavier:
Many packages also expose their version as a module attribute:
Not every package exposes __version__, so importlib.metadata is the reliable universal approach.
Package Name vs Import Name
A common source of confusion is that the name you install with pip is not always the name you import in Python. These are the distribution name and the import name, and they can differ:
| pip install name | Python import name | pip show uses |
beautifulsoup4 | bs4 | beautifulsoup4 |
Pillow | PIL | Pillow |
scikit-learn | sklearn | scikit-learn |
python-dateutil | dateutil | python-dateutil |
PyYAML | yaml | PyYAML |
opencv-python | cv2 | opencv-python |
If pip show bs4 returns nothing, try pip show beautifulsoup4. The pip show command always uses the distribution name (the name you use with pip install).
Verifying the Correct Environment
The single most common source of version confusion is checking the wrong environment. Here is a systematic way to verify:
If you are using a virtual environment, make sure it is activated first:
For conda environments:
Common Pitfalls
Checking the wrong Python interpreter. Running pip show against system Python while your project uses a virtualenv gives you the wrong version. Always use python -m pip with the same interpreter your application runs.
Confusing import names with distribution names. pip show sklearn fails because the distribution is named scikit-learn. Check the project's installation docs or use pip list to find the correct distribution name.
Trusting __version__ unconditionally. Not every package exposes this attribute, and some packages set it incorrectly in edge cases (like editable installs with stale metadata). importlib.metadata.version() reads from installed package metadata and is more reliable.
Using bare pip on systems with multiple Python versions. The pip command might resolve to Python 3.9 while your project requires Python 3.12. Always qualify with python -m pip or python3.12 -m pip.
Forgetting that editable installs can show stale versions. If a package is installed with pip install -e . (editable mode), pip show displays the version from the package metadata, which might not reflect uncommitted changes to the source.
Not checking Required-by before upgrading. Upgrading one package can break others that depend on a specific version range. Check pip show package-name and review the Required-by field before running pip install --upgrade.
Summary
- Use
python -m pip show package-namefor the clearest single-package version check, including location and dependency information. - Use
python -m pip listto browse all packages in the environment, with--outdatedto find upgradeable packages. - Use
python -m pip freezefor generating pinned dependency files for reproducibility. - Use
importlib.metadata.version("package")to check versions from within running Python code. - Always verify you are checking the correct Python interpreter and environment before trusting version output.
- Remember that distribution names (pip install) and import names (Python import) can differ for many popular packages.
Related reading
- Find which version of package is installed with pip
- Finding and replacing elements in a list
- Finding local IP addresses using Python's stdlib
- Finding median of list in Python
- Finding a branch point with Git?
- Finding diff between current and last version
- Finding the average of a list
- Finding the index of elements based on a condition using python list comprehension
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.