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.
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!