#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 把 i2c_inject_gui.py 打包成单文件 exe(工具名: I2C-Inject) ================================ 使用 PyInstaller --onefile --noconsole, 目标机器无需安装 Python/pyserial。 用法: python build_exe.py 自动安装 PyInstaller(如缺)并打包 产物: 本目录 dist/I2C-Inject.exe """ import os import subprocess import sys BASE = os.path.dirname(os.path.abspath(__file__)) ICO = os.path.join(BASE, 'logos', 'logo_i2c_orange.ico') def ensure_pyinstaller(): try: import PyInstaller # noqa: F401 return except ImportError: pass print('PyInstaller 未安装, 正在安装...') subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'pyinstaller']) def main(): ensure_pyinstaller() exe = os.path.join(BASE, 'dist', 'I2C-Inject.exe') if os.path.exists(exe): try: os.remove(exe) except PermissionError: subprocess.run(['taskkill', '/F', '/IM', 'I2C-Inject.exe'], capture_output=True) import time time.sleep(1.0) os.remove(exe) cmd = [sys.executable, '-m', 'PyInstaller', '--onefile', '--clean', '--name', 'I2C-Inject', '--distpath', os.path.join(BASE, 'dist'), '--workpath', os.path.join(BASE, 'build'), '--specpath', os.path.join(BASE, 'build'), '--noconsole', os.path.join(BASE, 'i2c_inject_gui.py')] if os.path.exists(ICO): # exe 文件图标 + 运行时窗口图标资源(从 _MEIPASS 读取) cmd += ['--icon', ICO, '--add-data', f'{ICO};.'] else: print('警告: 未找到 logos/logo_i2c_orange.ico(先运行 logos/make_ico_i2c.py), 本次打包不带图标') print('>>', ' '.join(cmd)) subprocess.check_call(cmd) print('完成: %s (%.1f MB)' % (exe, os.path.getsize(exe) / 1024 / 1024)) if __name__ == '__main__': main()