From b5ae10db531bae2d986a0b95c45c212b96d27e70 Mon Sep 17 00:00:00 2001 From: chun_qiu Date: Fri, 31 Jul 2026 13:17:09 +0800 Subject: [PATCH] Migrate from PyInstaller to Nuitka for compilation --- .gitignore | 2 +- build.ps1 | 50 +++++++++++++++++++++++++++++++------------------ config.py | 6 +++++- install_menu.py | 8 ++++++-- readme.md | 4 +++- 5 files changed, 47 insertions(+), 23 deletions(-) diff --git a/.gitignore b/.gitignore index 88ecac4..fd028ed 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,6 @@ dist/ build/ -*.spec +*.build logs/ cache/ assets/ diff --git a/build.ps1 b/build.ps1 index 2a6eaa4..810f1bd 100644 --- a/build.ps1 +++ b/build.ps1 @@ -4,46 +4,60 @@ .SYNOPSIS 壁纸更换器 - 编译脚本 .DESCRIPTION - 使用 PyInstaller 将 Python 源码打包为目录模式(--onedir)。 - 输出: .\dist\WallpaperChanger\WallpaperChanger.exe + 使用 Nuitka 将 Python 源码打包为独立目录模式(--standalone)。 + 输出: .\dist\WallpaperChanger.exe #> $ErrorActionPreference = "Stop" $ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path Write-Host "========================================" -ForegroundColor Cyan -Write-Host " 壁纸更换器 - 编译打包" -ForegroundColor Cyan +Write-Host " 壁纸更换器 - 编译打包 (Nuitka)" -ForegroundColor Cyan Write-Host "========================================" -ForegroundColor Cyan Write-Host "" -Write-Host "[1/4] 激活 conda 环境 ..." -ForegroundColor Yellow +Write-Host "[1/5] 激活 conda 环境 ..." -ForegroundColor Yellow conda activate 312 if ($LASTEXITCODE -ne 0) { throw "激活 conda 环境失败 (312)" } -Write-Host "[2/4] 安装编译依赖 ..." -ForegroundColor Yellow -python -m pip install pyinstaller --quiet -if ($LASTEXITCODE -ne 0) { throw "安装 PyInstaller 失败" } +Write-Host "[2/5] 安装编译依赖 ..." -ForegroundColor Yellow +python -m pip install nuitka --quiet +if ($LASTEXITCODE -ne 0) { throw "安装 Nuitka 失败" } python -m pip install -r "$ScriptDir\requirements.txt" --quiet -Write-Host "[3/4] 清理旧产物 ..." -ForegroundColor Yellow +Write-Host "[3/5] 清理旧产物 ..." -ForegroundColor Yellow Remove-Item -Recurse -Force "$ScriptDir\dist" -ErrorAction SilentlyContinue Remove-Item -Recurse -Force "$ScriptDir\build" -ErrorAction SilentlyContinue -Remove-Item -Force "$ScriptDir\*.spec" -ErrorAction SilentlyContinue -Write-Host "[4/4] PyInstaller 打包 ..." -ForegroundColor Yellow +Write-Host "[4/5] 生成图标(若无)..." -ForegroundColor Yellow +python -c " +import os; p = os.path.join('$ScriptDir', 'assets'); os.makedirs(p, exist_ok=True) +ico = os.path.join(p, 'icon.ico') +if not os.path.exists(ico): + from PIL import Image + img = Image.new('RGB', (64, 64), color=(66, 133, 244)) + img.save(ico, format='ICO', sizes=[(16,16),(32,32),(48,48),(64,64)]) +" + +Write-Host "[5/5] Nuitka 打包 ..." -ForegroundColor Yellow Set-Location -LiteralPath $ScriptDir -python -m PyInstaller ` - --onedir ` - --noconsole ` - --name "WallpaperChanger" ` - --clean ` +python -m nuitka ` + --standalone ` + --windows-disable-console ` + --enable-plugin=anti-bloat ` + --include-package=requests ` + --include-package=pystray ` + --include-package=PIL ` + --output-dir="$ScriptDir\dist" ` + --output-filename=WallpaperChanger.exe ` + --assume-yes-for-downloads ` "$ScriptDir\main.py" -if ($LASTEXITCODE -ne 0) { throw "PyInstaller 打包失败" } +if ($LASTEXITCODE -ne 0) { throw "Nuitka 打包失败" } Write-Host "" Write-Host "========================================" -ForegroundColor Green Write-Host " 编译完成!" -ForegroundColor Green -Write-Host " 输出: $ScriptDir\dist\WallpaperChanger\WallpaperChanger.exe" -ForegroundColor Green -Write-Host "========================================" -ForegroundColor Green \ No newline at end of file +Write-Host " 输出: $ScriptDir\dist\WallpaperChanger.dist\WallpaperChanger.exe" -ForegroundColor Green +Write-Host "========================================" -ForegroundColor Green diff --git a/config.py b/config.py index b53c67b..afe8299 100644 --- a/config.py +++ b/config.py @@ -3,8 +3,12 @@ import os import sys +def _is_frozen(): + return getattr(sys, 'frozen', False) or '__compiled__' in sys.modules + + def _get_app_dir(): - if getattr(sys, 'frozen', False): + if _is_frozen(): return os.path.dirname(sys.executable) return os.path.dirname(os.path.abspath(__file__)) diff --git a/install_menu.py b/install_menu.py index 8c9a35d..a8acdbd 100644 --- a/install_menu.py +++ b/install_menu.py @@ -11,14 +11,18 @@ COMMAND_KEY = REG_KEY + r"\command" MENU_NAME = "刷新壁纸(&R)" +def _is_frozen() -> bool: + return getattr(sys, 'frozen', False) or '__compiled__' in sys.modules + + def _get_app_dir() -> str: - if getattr(sys, 'frozen', False): + if _is_frozen(): return os.path.dirname(sys.executable) return os.path.dirname(os.path.abspath(__file__)) def _get_command() -> str: - if getattr(sys, 'frozen', False): + if _is_frozen(): return f'"{sys.executable}" --refresh' else: base = os.path.dirname(sys.executable) diff --git a/readme.md b/readme.md index 71fcf9a..4e28376 100644 --- a/readme.md +++ b/readme.md @@ -28,11 +28,13 @@ python main.py --refresh ## 编译为 .exe +使用 Nuitka 打包: + ```powershell powershell -ExecutionPolicy Bypass -File build.ps1 ``` -产物:`dist/WallpaperChanger.exe` +产物:`dist/WallpaperChanger.dist/WallpaperChanger.exe`(独立目录模式,含 exe + 依赖库) - 双击运行 → 启动托盘 - `WallpaperChanger.exe --refresh` → 拉取并设置壁纸后退出(右键菜单用)