Method 1: sys.version
To check your version at runtime in your code, import the sys
module and print the sys.version
attribute to your Python shell:
import sys print(sys.version) # 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)]
Method 2: sys.version_info
If you need an easy-to-process output for the major, minor, and micro versions, use the sys.version_info
attribute. For example, Python version 3.9.5 has major version 3, minor version 9, and micro version 5. You can access the major version with sys.version_info[0]
, the minor version with sys.version_info[1]
, and the micro version with sys.version_info[2]
.
import sys print(sys.version_info) # sys.version_info(major=3, minor=9, micro=5, releaselevel='final', serial=0) print(sys.version_info[0]) # Major: 3 print(sys.version_info[1]) # Minor: 9 print(sys.version_info[2]) # Micro: 5
Method 3: platform.python_version()
The platform.python_version()
function returns a string representation of the form 'major:minor:micro'
. For example, you can easily split it on the dots to obtain the respective major, minor, or micro version info returned by this function.
import platform print(platform.python_version()) # 3.9.5