
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How I can check a Python module version at runtime?
Python introduces new features with every latest version. Therefore, to check what version is currently being used we need a way to retrieve them. If a user implements features without the correct version, it can generate random issues.
We can check the version using few different methods. These methods are discussed below.
Using the Sys Module
Using sys module, we can retrieve the current python module version. The following lines of code can be used to do so.
import sys print(sys.version)
Output
The output obtained is as follows.
3.10.6 (main, Mar 10 2023, 10:55:28) [GCC 11.3.0]
Example
Using sys module to retrieve the current python module version
print(sys.version_info)
Output
The output is as follows.
sys.version_info(major=3, minor=8, micro=8, releaselevel='final', serial=0)
Example
Using sys module to find out the current version of the python module.
import sys print(sys.version_info.major) print(sys.version_info.minor) print(sys.version_info.micro)
Output
The output is as follows.
3 10 6
Using Platform module
Using Platform module, we can retrieve the current python module version.
import platform print(platform.python_version()) print("Current Python version is: "+platform.python_version())
Output
3.8.8 Current Python version is: 3.8.6
Example
In this example, we will see how to print the current version of the module separately instead of as a single input. The working can be seen below.
import platform print(platform.python_version_tuple()[0]) print(platform.python_version_tuple()[1]) print(platform.python_version_tuple()[2])
Output
The output generated is as follows.
3 8 8
The pkg_resources module
Use pkg_resources module to get the module version at runtime. Anything installed from PyPI at least should have a version number.
import pkg_resources pkg_resources.get_distribution("blogofile").version
Note that package name must be that of PyPI entry.So something like "pkg_resources.get_distribution('MySQLdb').version" won't workbut "pkg_resources.get_distribution('mysql-python').version" will.
- Related Articles
- How I can check a Python module version at runtime?
- How can we define a Python function at runtime?
- How can I check the version of MySQL Server?
- How I can dynamically import Python module?
- How I can install unidecode python module on Linux?
- How to display Java Runtime Environment (JRE) version
- How do I check what version of Python is running my script?
- How to install the specific version of the PowerShell module version?
- How to check version of python modules?
- Can we change Python for loop range (higher limit) at runtime?
- How do I unload (reload) a Python module?
- How to install the latest PowerShell module version?
- How to install and import Python modules at runtime?
- Can I import same package twice? Will JVM load the package twice at runtime?
- How to check if a python module exists without importing it?
- How do I change current theme at runtime in my android app?
