Files
ykxmz/_build/check_win7_compat.py
xmz7891 be59a20036 2.1.5:修「Windows 7 装完报 dll 缺失」
用户报:学校的电脑是 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+ 生效)。

所以之前用 3.11 打出来的包,在 Win7 上必然报缺 DLL ——
**那个 DLL 不是"忘了放进包",是"放进去也没用"。**

顺带说明:包里那 47 个 api-ms-win-core-*.dll 转发库正是
Python 为兼容 Win7 才带上的 —— 上游本来就考虑过 Win7,
只是 3.11 这个版本号越界了。

== 改法 ==

1. **改用 Python 3.8 打包,而且选 32 位**
   32 位在 Win7 的 32 位和 64 位系统上都能跑,学校不用分两种发。
   (64 位的包在 32 位 Windows 上直接起不来。)

   依赖固定到 3.8 能用的版本:
       Pillow==9.5.0(最后一个支持 3.8 的)
       pyinstaller 6.x(保持 _internal 布局,和安装包/测试/卸载逻辑一致;
                       5.x 会把依赖摊在根目录,导致"_internal 在"那一项失败)

2. **打包脚本主动拦**
   windows\build_exe.ps1 打包前先校验 Python:
     * 找不到 3.8 -> 报错并给出安装命令
     * 版本不是 3.8 -> 直接 throw,而不是默默打一个别人装不上的包
     * 不是 32 位 -> 警告
   Python 从 YKXMZ_PY 环境变量或常见路径里找,不写死。

3. **新增 Win7 兼容校验** _build\check_win7_compat.py
   盯三件事:内嵌是不是 3.8 的运行库、**有没有**那个要命的 DLL、
   exe 是不是 32 位。打包时和回归套件(3a9 关)里都会跑,
   不过就拒绝出包。

== 顺手修的 ==

_build/test_installer_cycle.py 里写死了 ykxmz-setup-1.8.2.exe,
于是一直在**装一个不存在的旧包** -> 静默失败 -> 报出一堆
莫名其妙的失败("配对码实际是 xxx"、"版本显示 v1.8.2")。
改成自动找 dist 下版本号最新的包(按数字段排序,不能按字符串,
否则 1.9.0 会排到 2.1.4 前面)。

== 验证(用户要求:自己测一遍,没问题再上传)==

    全量回归 42 个阶段全 PASS(含新的 "Windows 7 兼容" 关)
    安装包完整周期 20/20
    Win7 校验:python38.dll / 无 api-ms-win-core-path / 32 位
    实际启动打包的 EXE:进程活着、TCP 监听、UDP 自动发现在听、
                        连接能建立(日志里有"设备接入")
    产物扫描:三件全部干净(无旧密码、无服务器 IP)
    提交前泄漏闸门通过

安装包体积 14.4 MB -> 9.0 MB。
2026-10-09 13:16:31 +08:00

138 lines
4.6 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- 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("<I", f.read(4))[0]
f.seek(off)
if f.read(4) != b"PE\0\0":
return 0
return struct.unpack("<H", f.read(2))[0]
def main() -> 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())