检查Python版本、操作系统类型以及是否已安装指定的包

import sys
import platform
import subprocess
 
# 检查Python版本
def check_python_version():
    if sys.version_info < (3, 6):
        print("Python版本低于3.6,无法执行此脚本。")
        sys.exit(1)
 
# 检查操作系统类型
def check_os_type():
    if sys.platform != 'win32':
        print("此脚本仅支持Windows操作系统。")
        sys.exit(1)
 
# 检查是否已安装指定的包
def check_package_installed(package_name):
    try:
        result = subprocess.run(['pip', 'show', package_name], capture_output=True, text=True)
        if result.returncode == 0:
            print(f"{package_name} 已经安装。")
            return True
        else:
            print(f"{package_name} 未安装。")
            return False
    except Exception as e:
        print(f"检查 {package_name} 是否安装时出现错误:{e}")
        return False
 
# 主函数
def main():
    check_python_version()
    check_os_type()
    package_name = 'numpy'  # 要检查的包名称
    if not check_package_installed(package_name):
        print(f"开始安装 {package_name}...")
        subprocess.run(['pip', 'install', package_name])
 
if __name__ == "__main__":
    main()