# -*- coding: utf-8 -*- r'''校验打出来的 EXE 能不能在 **Windows 7** 上跑。 == 为什么需要这个 == 学校电脑是 Windows 7,装完报「dll 缺失」。 根因不是漏打包,是 **Python 版本越界**: Python 3.8 -> 最后一个支持 Windows 7 的版本 Python 3.9+ -> 只支持 Windows 8 及以上 Python 3.9 起用到了 `api-ms-win-core-path-l1-1-0.dll` 里的 `PathCchCanonicalizeEx`,而这个 DLL **Windows 7 上根本不存在**, 也没法用 api-set 转发(那套机制只在 Win8+ 生效)。 **所以那个 DLL 不是"忘了放进包",是"放进去也没用"。** 唯一可行的办法是**用 3.8 打包**。 == 这个脚本查什么 == 1. 有没有 python38.dll(而不是 python39/310/311/... ) 2. **不该有** api-ms-win-core-path-l1-1-0.dll —— 有它就说明是 3.9+ 打的,Win7 上必挂 3. PE 头是不是 32 位(0x14C) —— 32 位在 Win7 的 32/64 位系统上都能跑; 64 位在 32 位 Windows 上直接起不来 4. 顺带报一下总大小和文件数 用 `python _build/check_win7_compat.py` 单独跑也行, `build_exe.ps1` 里会调它,不过就直接拒绝出包。 ''' from __future__ import annotations import struct import sys from pathlib import Path ROOT = Path(__file__).resolve().parent.parent DIST = ROOT / "dist_exe" / "ykxmz" FAILS: list = [] def check(name: str, ok: bool, why: str = "") -> None: print(" [%s] %s%s" % ("PASS" if ok else "FAIL", name, (" —— " + why) if why else "")) if not ok: FAILS.append(name) def pe_machine(path: Path) -> int: """读 PE 头里的 machine 字段。0x14C=32 位,0x8664=64 位。""" with path.open("rb") as f: if f.read(2) != b"MZ": return 0 f.seek(0x3C) off = struct.unpack(" int: print("=" * 58) print(" Windows 7 兼容性校验") print("=" * 58) print() check("dist_exe/ykxmz 存在", DIST.is_dir(), "先跑 windows\\build_exe.ps1") if not DIST.is_dir(): return 1 dlls = {p.name.lower(): p for p in DIST.rglob("*.dll")} exe = DIST / "ykxmz.exe" check("ykxmz.exe 存在", exe.is_file()) if not exe.is_file(): return 1 # ---- 1. Python 运行库版本 ---- print() print("一、Python 运行库") py_dlls = sorted(n for n in dlls if n.startswith("python3") and n.endswith(".dll")) for n in py_dlls: print(" %s" % n) check("有 python38.dll(3.8 打的)", any("python38" in n for n in py_dlls), "没有 python38.dll 说明不是 3.8 打的") bad_py = [n for n in py_dlls if any(v in n for v in ("python39", "python310", "python311", "python312", "python313", "python314"))] check("没有 3.9+ 的运行库", not bad_py, "3.9+ 的包在 Win7 上会因为缺 DLL 启动失败:%s" % bad_py) # ---- 2. 那个要命的 DLL ---- print() print("二、Win7 上不存在的那个 DLL") killer = "api-ms-win-core-path-l1-1-0.dll" has = killer in dlls check("**没有** %s" % killer, not has, "有它就说明是 3.9+ 打的 —— Win7 上必然报缺 DLL") print(" (这个 DLL 是 Win8+ 才有的。Python 3.9 起用到它,") print(" 放进来也没用,Win7 上加载不了。)") # ---- 3. 位数 ---- print() print("三、位数") m = pe_machine(exe) arch = {0x14C: "32 位 (x86)", 0x8664: "64 位 (x64)"}.get(m, "未知 (0x%X)" % m) print(" ykxmz.exe: %s" % arch) check("是 32 位(Win7 32/64 位都能跑)", m == 0x14C, "64 位的包在 32 位 Windows 上起不来;学校机器可能是 32 位") # ---- 4. 体积概览 ---- print() print("四、产物概览") files = list(DIST.rglob("*")) files = [f for f in files if f.is_file()] total = sum(f.stat().st_size for f in files) print(" 文件数: %d" % len(files)) print(" 总大小: %.1f MB" % (total / 1048576)) print(" exe 大小: %.1f MB" % (exe.stat().st_size / 1048576)) print() print("=" * 58) if FAILS: print(" 失败 %d 项:%s" % (len(FAILS), ", ".join(FAILS))) print(" 这个包学校电脑装不上,不该发布。") return 1 print(" ✅ 可以在 Windows 7 / 8 / 10 / 11 上运行") return 0 if __name__ == "__main__": sys.exit(main())