Python Check the Python version
Python: View Python Version
Python is a powerful and popular high-level programming language that supports multiple platforms and has a wide range of applications, including web development, data analysis, and artificial intelligence. When learning and developing Python programs, you may need to know the current Python version to determine code compatibility and feature support. This article introduces several common methods for checking the Python version, along with corresponding example code and running results.
Method 1: Using the sys Module
The sys module is a built-in module in the Python standard library. It provides interpreter-related functions and variables. The sys.version variable can be used to check the current Python interpreter version.
import sys
print(sys.version)
Running the above code will output the current Python interpreter version information, for example:
3.9.4 (default, Apr 9 2021, 18:42:22)
[GCC 8.4.0]
In this example, Python version is 3.9.4.
Method 2: Using the platform module
The platform module is a built-in module in the Python standard library that provides functions for accessing information about the underlying operating system. The platform.python_version() function can be used to view the current Python interpreter version.
import platform
print(platform.python_version())
Running the above code will output the current Python interpreter version information, for example:
3.9.4
Method 3: Using the sysconfig Module
sysconfig is a built-in module in the Python standard library that provides access to configuration information related to the current Python interpreter. The sysconfig.get_python_version() function can be used to view the current Python interpreter version.
import sysconfig
print(sysconfig.get_python_version())
Running the above code will output the current Python interpreter version information, for example:
3.9.4
Method 4: Use the platform module to obtain more detailed version information
In addition to viewing the basic Python version number, sometimes we need to obtain more detailed version information, such as the distribution and compiler. The platform module provides several other functions to meet these needs. For example, the platform.python_build() function can be used to obtain Python interpreter build information.
import platform
print(platform.python_build())
Running the above code will output the build information of the current Python interpreter, for example:
('default', 'Apr 9 2021 18:42:22')
In this example, the build information is (‘default’, ‘Apr 9 2021 18:42:22’).
Summary
This article introduced four common methods for checking the Python version. Using the sys module, the platform module, and the sysconfig module, we can easily obtain the version information of the current Python interpreter. These methods are very useful when developing and debugging Python programs, making it easy to determine code compatibility and feature support.