- Check the version of Python package/library
- Get package version in Python script: __version__ attribute
- Check package version with pip command: pip list , pip freeze , pip show
- List installed packages: pip list
- List installed packages: pip freeze
- Check details of installed packages: pip show
- Check package version with conda command: conda list
- Check Python Module Version
- Use the __version__ Method to Find the Version of a Module in Python
- Use the importlib.metadata Module to Find the Version of a Module in Python
- Use the pkg_resources Module to Find the Version of a Module in Python
- Use the pip show Command to Find the Version of a Module in Python
- Related Article — Python Module
- How to Check Version of Installed Python Modules
- Checking the version of Python modules
- Method 1: using pip freeze
- Method 2: Using pip list
- Method 3: Using pip show
- Method 4: using package.__version__ function
- Method 5: Using importlib from python
- Conclusion
Check the version of Python package/library
This article explains how to check the versions of packages (libraries) and modules used in Python scripts, and the versions of packages installed in your environment.
If you want to check the version of Python itself, see the following article.
Get package version in Python script: __version__ attribute
To get the version of a package used in a Python script, use the __version__ attribute.
import pandas as pd print(pd.__version__) # 2.0.1
The __version__ attribute, recommended by Python Enhancement Proposals (PEP), is available in many packages.
Note that not all packages have the __version__ attribute because it is not mandatory.
In addition to the __version__ attribute, some packages, such as NumPy and pandas, provide functions and attributes that display more detailed information.
Note that the __version__ is not set for the standard library modules such as math and os . Modules in the standard library do not have individual versions but follow the Python version.
Check package version with pip command: pip list , pip freeze , pip show
If you are using the Python package management system, pip , you can check the information about the installed packages using the following commands. Run these commands in the command prompt or terminal.
In some environments, use pip3 instead of pip . In some cases, pip is for Python2, and pip3 is for Python3.
For basic information on how to use pip, such as installing, updating, and uninstalling packages, please see the following article.
List installed packages: pip list
pip list displays a list of installed package names and version numbers.
$ pip list Package Version ------------------ --------- absl-py 0.1.10 agate 1.6.0 agate-dbf 0.2.0 agate-excel 0.2.1 agate-sql 0.5.2 appnope 0.1.0 .
The pip list command also accepts the following options:
- —format
- Set the display format ( columns , freeze , json )
- Show only out-of-date packages
- Show only the latest packages
Refer to the following article for more information.
List installed packages: pip freeze
pip freeze displays a list of installed package names and version numbers in freeze format.
$ pip freeze absl-py==0.1.10 agate==1.6.0 agate-dbf==0.2.0 agate-excel==0.2.1 agate-sql==0.5.2 appnope==0.1.0 .
The difference between pip freeze and pip list —format freeze is that pip freeze does not show the following package management tools by default.
You can display them all by adding the —all option.
By saving the output in freeze format to a text file, you can install packages in a specified version all at once.
In such cases, listing package management tools like pip isn’t necessary, hence pip freeze omits them by default.
Check details of installed packages: pip show
pip show displays detailed information about a given package.
In addition to version information, detailed information such as license and dependency packages is displayed.
$ pip show pandas Name: pandas Version: 2.0.1 Summary: Powerful data structures for data analysis, time series, and statistics Home-page: Author: Author-email: The Pandas Development Team License: BSD 3-Clause License . Location: /opt/homebrew/lib/python3.11/site-packages Requires: numpy, numpy, python-dateutil, pytz, tzdata Required-by:
Check package version with conda command: conda list
If you have built a Python environment with Anaconda, conda list will list the packages installed in the current virtual environment.
When the environment is not activated, use conda list -n .
Check Python Module Version
- Use the __version__ Method to Find the Version of a Module in Python
- Use the importlib.metadata Module to Find the Version of a Module in Python
- Use the pkg_resources Module to Find the Version of a Module in Python
- Use the pip show Command to Find the Version of a Module in Python
It is usually recommended to use the pip command to install Python modules. It is because, using the pip command, we can specify the required version of the module which we wish to install.
Modules are updated regularly. New functions and features are added regularly, and some also get deprecated, which may lead to errors if one is not aware of these changes. Thus, it is essential to be in knowledge of what version of the module is installed.
In this tutorial, we will discuss how to check for the version of a module in Python.
Use the __version__ Method to Find the Version of a Module in Python
Usually, most of the modules have the __version__ method associated with them, revealing its version.
import numpy print(numpy.__version__)
However, it is not advisable to use this method. First off, __version__ is a magic method that is usually not meant to be called explicitly. Secondly, not every module has this attribute that can tell its version.
Use the importlib.metadata Module to Find the Version of a Module in Python
In Python v3.8 and above, we have the importlib.metadata module, which has the version() function. This function will return the version of the specified module.
from importlib_metadata import version print(version('numpy'))
We can also use the import_metadata module for older versions of Python.
Use the pkg_resources Module to Find the Version of a Module in Python
Below Python 3.8, we can use the get_distribution.version() method from the pkg_resources module to find a module version. Note that the string that you pass to the get_distribution method should correspond to the PyPI entry.
import pkg_resources print(pkg_resources.get_distribution('numpy').version)
Use the pip show Command to Find the Version of a Module in Python
Alternatively, we can use the pip show command to find out details about a specific package that includes its version.
Note that pip should be updated for this.
Manav is a IT Professional who has a lot of experience as a core developer in many live projects. He is an avid learner who enjoys learning new things and sharing his findings whenever possible.
Related Article — Python Module
How to Check Version of Installed Python Modules
One of the most useful features of Python is its huge collection of in-built modules. Modules make everything easier. Due to the availability of numerous python libraries, implementing python code using the in-built modules has increasingly become hassle-free.
But, when we are importing certain modules, we must ensure that they are up-to-date, or else they might raise unwanted errors. In order to write flawless code, we must ensure that the modules are at par with the latest python version in our system.
Checking the version of Python modules
The version of our installed python libraries can be checked and further updated using the pip module. There are many ways in which you can ensure that your system is up to date with the latest release version in order to avoid missing out on exciting new features.
Let us look at how we can check the versions of installed python modules.
Make sure your pip version is >= 1.3! If your pip is not updated, make sure you update it.
If you want to read more about python modules, read this awesome article on Introduction to Python Modules.
Method 1: using pip freeze
The pip freeze method lists all the installed packages and their versions in an alphabetical order.
Open your command prompt and run the following command:
Your output will look something like this:
C:\Users\SHREYA>pip freeze contourpy==1.0.6 cycler==0.11.0 DateTime==5.0 et-xmlfile==1.1.0 fonttools==4.38.0 joblib==1.2.0 kiwisolver==1.4.4 matplotlib==3.6.2 numpy==1.24.1 openpyxl==3.0.10 packaging==23.0 pandas==1.5.2 Pillow==9.4.0 playsound==1.3.0 psutil==5.9.4 pydub==0.25.1 pygame==2.1.3 pyparsing==3.0.9 python-dateutil==2.8.2 pytz==2022.7 scikit-learn==1.2.1 scipy==1.10.0 seaborn==0.12.2 six==1.16.0 threadpoolctl==3.1.0 tk==0.1.0 zope.interface==5.5.2
Method 2: Using pip list
The pip list method also works in a similar manner. It will list all the python modules installed in your system followed by their versions that are locally available on your system.
Run the following code to see the entire list of the modules and their versions on your system.
Method 3: Using pip show
Using pip show, we can determine the version of one specified module according to our needs. Lets look at how this can be done:
Run the following code in your command prompt: pip show numpy (OR ANY OTHER MODULE VERSION THAT YOU WANNA KNOW)
The output will e something as shown below:
C:\Users\SHREYA>pip show numpy Name: numpy Version: 1.24.1 Summary: Fundamental package for array computing in Python Home-page: https://www.numpy.org Author: Travis E. Oliphant et al. Author-email: License: BSD-3-Clause Location: C:\Users\SHREYA\AppData\Local\Programs\Python\Python311\Lib\site-packages Requires: Required-by: contourpy, matplotlib, pandas, scikit-learn, scipy, seaborn
To know more about PIP, check out: Python PIP – Package Manager.
Method 4: using package.__version__ function
This method can be used in the python shell to display the version of a particular library. We can implement this method as follows:
import numpy print(numpy.__version__) #displaying version of the numpy
I have used numpy here, you can modify it according to your use. The output would be simply the version of numpy installed in your system.
Method 5: Using importlib from python
There is a python module called importlib(also called ,import library) which can be used to check the version of a particular module in our python script or terminal. The importlib package contains a function called metadata which is useful for extracting information about modules.
Before you use this package, make sure you have it in your system. If not, run pip install importlib in your command prompt in administrator mode as to maintain all PATH properly.
Now, we will look at how to use this package. I will run my code in the python shell.
import importlib.metadata print(importlib.metadata.version('DateTime'))
I already had the DateTime module installed, that’s why I am using it to check the proper functionality of the importlib.metadata.version() . You can use whatever module you want to know the version of.
The output of the above code will be the version of your required module. In this case, 5.0 is the output that I got, because that version of DateTime exists locally.
Conclusion
In this tutorial, we have seen how to determine the versions of the installed python packages in our system. This can be done in five different ways and each of those methods are extremely easy to use.
The simple syntax of python is one of the biggest reasons why it is so popular. Besides the syntax, the huge collection of in-built functions and modules make programming in this language extremely efficient even at a large scale. Hence, it is important to keep all modules up-to-date so that we can maximize our productivity and make the best use of newly added features!