import os import subprocess import sys import shutil # 确保我们在虚拟环境中运行 if not sys.prefix == sys.base_prefix: print(f"正在使用虚拟环境: {sys.prefix}") else: print("警告:你似乎没有激活虚拟环境!建议在 venv_clean 下运行。") def build(): entry_point = "runDecoder.py" # 你的入口文件 # 自动清理逻辑优化 output_dir = "dist2" build_dir = "build2" # Nuitka 默认会在当前目录生成 .build 文件夹 if "--clean" in sys.argv: print("清理旧构建目录...") for folder in [output_dir, build_dir, entry_point.replace(".py", ".build")]: if os.path.exists(folder): shutil.rmtree(folder, ignore_errors=True) # Nuitka 命令 - 此时非常清爽 nuitka_cmd = [ sys.executable, "-m", "nuitka", "--standalone", # 独立运行模式 f"--output-dir={output_dir}", # 输出目录 "--show-progress", # 显示进度 "--assume-yes-for-downloads", # 自动下载依赖(如 ccache, depends 等) # --- 插件配置 --- "--enable-plugin=numpy", "--enable-plugin=matplotlib", "--enable-plugin=torch", # 处理 PyTorch 及其 CUDA 依赖 # --- 包含包/模块 (Nuitka 2.x 推荐使用 include-package-data 或 include-package) --- # --collect-all 是 PyInstaller 的参数,Nuitka 不支持 "--include-package=sklearn", "--include-package=scipy", "--include-package=mne", # 强制包含 MNE 的数据文件(配置、布局等) "--include-package-data=mne", "--include-package=PIL", # Pillow (matplotlib/mne 可能用到) "--include-package=networkx", # mne 可能用到 "--include-package=decorator", # MNE 核心依赖,防止 KeyError: 'self' "--include-package=six", # 通用兼容库 # 显式包含本地模块,防止隐式导入丢失 "--include-module=infer_pth", # --- 数据文件 --- # 格式: 源路径=目标路径 (相对 dist 目录) "--include-data-dir=model=model", "--include-data-dir=raw_data=raw_data", # --- 排除干扰以减小体积/提高稳定性 --- "--nofollow-import-to=pytest", "--nofollow-import-to=unittest", "--nofollow-import-to=pdb", "--nofollow-import-to=tkinter", # 如果不用 GUI 界面 "--nofollow-import-to=sympy", # 除非明确用到符号计算 # --- 内存与性能 --- "--low-memory", # 降低打包时的内存消耗 # --- Windows 特定 --- # "--disable-console", # 如果不需要黑框,取消注释这一行 ] nuitka_cmd.append(entry_point) print("开始打包...") try: subprocess.check_call(nuitka_cmd) print("\n打包成功!") print(f"请在 dist2/runDecoder.dist 目录下运行 exe 进行测试。") except subprocess.CalledProcessError as e: print(f"打包失败,错误码: {e.returncode}") if __name__ == "__main__": build()