In this video course, we will provide a detailed analysis of how to efficiently detect and extract version information of the running environment being used from Python programs. Whether you are a beginner in the programming field or a seasoned veteran, understanding the Python version that a project depends on is fundamental and crucial knowledge. The course will demonstrate how to quickly obtain comprehensive environment version information using Python’s built-in toolkits (such as’ subprocess’, ‘sys’, and’ platform ‘) through three practical techniques.
1. Example Video.
2. Python Source Code.
import subprocess import sys import platform def get_python_version_by_subprocess(): result = subprocess.run('python -V',capture_output=True, text=True) version_str = result.stdout print(version_str) tmp_list = version_str.strip().split(' ') version_list = tmp_list[1].split('.') print(version_list) def get_python_version_by_sys(): version_str = sys.version print(version_str) version_info = sys.version_info print(version_info) print(version_info.major) print(version_info.minor) print(version_info.micro) def get_python_version_by_platform(): python_version = platform.python_version() print('python_version :', python_version) python_version_tuple = platform.python_version_tuple() print('python_version_tuple :', python_version_tuple) python_branch = platform.python_branch() print('python_branch :', python_branch) python_implementation = platform.python_implementation() print('python_implementation :', python_implementation) python_compiler = platform.python_compiler() print('python_compiler :', python_compiler) python_build = platform.python_build() print('python_build :', python_build) python_revision = platform.python_revision() print('python_revision :', python_revision) if __name__ == "__main__": #get_python_version_by_subprocess() #get_python_version_by_sys() get_python_version_by_platform()