Knowing the version of a Python package is essential for debugging, dependency management, reproducibility, and ensuring compatibility. In this guide, you’ll learn multiple reliable ways to check package versions using both the command line and Python code.
1. Check Version with importlib.metadata (Python 3.8+)
This is the cleanest and most modern method to get the version programmatically:
from importlib.metadata import version
print(version("numpy"))
This works even if the package doesn’t define __version__.
2. Use pip show to Get Version and Package Info
Open a terminal and run:
pip show pandas
This shows detailed metadata including:
- Package name
- Version
- Location
- Dependencies
This is useful when debugging environments. ([TechBloat][2])
3. List All Installed Packages and Versions
To see every installed package with its version:
pip list
or for Python 3 specifically:
pip3 list
This is great for auditing your environment. ([TechBloat][2])
4. Check Version from within Python
Many packages expose a version attribute:
import pandas as pd
print(pd.__version__)
This is quick and easy if the package supports __version__. ([Stack Overflow][1])
5. Why To Check Package Versions
- Reproducibility:
- Ensures your code runs the same on other machines.
- Dependency management:
- Helps avoid conflicting versions.
- Debugging:
- Many errors are version related.
- CI/CD workflows:
- Useful in automated testing and deployment.
6. Tips for Best Results
- Always check versions inside your virtual environment to avoid conflicts.
- Use
pip freeze > requirements.txtto lock versions. - If a package doesn't expose a version, use metadata (
importlib.metadata).
Summary
| Method | Best For |
|---|---|
importlib.metadata.version() |
Programmatic checks |
pip show |
Detailed package info |
pip list |
Full environment audit |
__version__ attribute |
Quick interactive check |
7. Example for pandas
(1) Check single package version - Python > 3.8
from importlib.metadata import version
version('pandas')
result:
'2.0.0.dev2'
(2) Check single package version - pip freeze and grep
pip freeze | grep pandas
result:
pandas==2.0.0.dev2
(3) Check all packages in current environment
pip freeze
(4) Check package version and package details (requirements etc)
!pip show moviepy
!pip show moviepy | grep Version
result of first command is:
Name: moviepy
Version: 2.0.0.dev2
Summary: Video editing with Python
Home-page: https://zulko.github.io/moviepy/
Author: Zulko 2017
Author-email:
License: MIT License
Location: /home/user/Software/Tensorflow/environments/venv38/lib/python3.8/site-packages
Requires: requests, proglog, imageio-ffmpeg, numpy, imageio, decorator
Required-by:
(5) Show current used package version
If you install a new version but the previous one was imported. This will result in displaying which one is currently used.
import pandas
pandas.__version__
result:
'2.0.3'
(5) Install specific pre-release version in Python
pip install 'pandas>=1.0.3' --pre
To check which is the installed version of a given Python package use:
from importlib.metadata import version
version('pandas')
result:
'2.0.0.dev2'
You can read more about pip install here: