Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to check version of python modules?
When you install Python, you also get the Python package manager, pip. You can use pip to get the versions of python modules and check them programmatically within your Python scripts.
Using pip freeze Command
If you want to list all installed Python modules with their version numbers, use the following command ?
$ pip freeze
You will get the output ?
asn1crypto==0.22.0 astroid==1.5.2 attrs==16.3.0 Automat==0.5.0 backports.functools-lru-cache==1.3 cffi==1.10.0 ...
Finding Specific Module Version
On Linux/macOS (using grep)
To individually find the version number you can grep on this output on *NIX machines ?
$ pip freeze | grep PyMySQL
PyMySQL==0.7.11
On Windows (using findstr)
On Windows, you can use findstr instead of grep ?
PS C:\> pip freeze | findstr PyMySQL
PyMySQL==0.7.11
Checking Version Within Python Script
If you want to know the version of a module within a Python script, you can use the __version__ attribute of the module. Note that not all modules come with a __version__ attribute ?
import requests print(requests.__version__)
2.28.1
Alternative Methods
Using pip show Command
You can get detailed information about a specific package ?
$ pip show numpy
Name: numpy Version: 1.21.0 Summary: NumPy is the fundamental package for array computing with Python. ...
Using importlib.metadata (Python 3.8+)
For a more reliable way to check versions programmatically ?
import importlib.metadata
version = importlib.metadata.version('numpy')
print(f"NumPy version: {version}")
NumPy version: 1.21.0
Comparison
| Method | Use Case | Reliability |
|---|---|---|
pip freeze |
List all packages | High |
__version__ |
Within Python script | Medium (not always available) |
importlib.metadata |
Within Python script | High (Python 3.8+) |
pip show |
Detailed package info | High |
Conclusion
Use pip freeze for listing all packages, pip show for detailed package information, and importlib.metadata for reliable version checking within Python scripts. The __version__ attribute works but isn't always available.
