Migrate from PyInstaller to Nuitka for compilation
This commit is contained in:
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
dist/
|
dist/
|
||||||
build/
|
build/
|
||||||
*.spec
|
*.build
|
||||||
logs/
|
logs/
|
||||||
cache/
|
cache/
|
||||||
assets/
|
assets/
|
||||||
|
|||||||
@@ -4,46 +4,60 @@
|
|||||||
.SYNOPSIS
|
.SYNOPSIS
|
||||||
壁纸更换器 - 编译脚本
|
壁纸更换器 - 编译脚本
|
||||||
.DESCRIPTION
|
.DESCRIPTION
|
||||||
使用 PyInstaller 将 Python 源码打包为目录模式(--onedir)。
|
使用 Nuitka 将 Python 源码打包为独立目录模式(--standalone)。
|
||||||
输出: .\dist\WallpaperChanger\WallpaperChanger.exe
|
输出: .\dist\WallpaperChanger.exe
|
||||||
#>
|
#>
|
||||||
|
|
||||||
$ErrorActionPreference = "Stop"
|
$ErrorActionPreference = "Stop"
|
||||||
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
|
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
|
||||||
|
|
||||||
Write-Host "========================================" -ForegroundColor Cyan
|
Write-Host "========================================" -ForegroundColor Cyan
|
||||||
Write-Host " 壁纸更换器 - 编译打包" -ForegroundColor Cyan
|
Write-Host " 壁纸更换器 - 编译打包 (Nuitka)" -ForegroundColor Cyan
|
||||||
Write-Host "========================================" -ForegroundColor Cyan
|
Write-Host "========================================" -ForegroundColor Cyan
|
||||||
Write-Host ""
|
Write-Host ""
|
||||||
|
|
||||||
Write-Host "[1/4] 激活 conda 环境 ..." -ForegroundColor Yellow
|
Write-Host "[1/5] 激活 conda 环境 ..." -ForegroundColor Yellow
|
||||||
conda activate 312
|
conda activate 312
|
||||||
if ($LASTEXITCODE -ne 0) { throw "激活 conda 环境失败 (312)" }
|
if ($LASTEXITCODE -ne 0) { throw "激活 conda 环境失败 (312)" }
|
||||||
|
|
||||||
Write-Host "[2/4] 安装编译依赖 ..." -ForegroundColor Yellow
|
Write-Host "[2/5] 安装编译依赖 ..." -ForegroundColor Yellow
|
||||||
python -m pip install pyinstaller --quiet
|
python -m pip install nuitka --quiet
|
||||||
if ($LASTEXITCODE -ne 0) { throw "安装 PyInstaller 失败" }
|
if ($LASTEXITCODE -ne 0) { throw "安装 Nuitka 失败" }
|
||||||
|
|
||||||
python -m pip install -r "$ScriptDir\requirements.txt" --quiet
|
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\dist" -ErrorAction SilentlyContinue
|
||||||
Remove-Item -Recurse -Force "$ScriptDir\build" -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
|
Set-Location -LiteralPath $ScriptDir
|
||||||
python -m PyInstaller `
|
python -m nuitka `
|
||||||
--onedir `
|
--standalone `
|
||||||
--noconsole `
|
--windows-disable-console `
|
||||||
--name "WallpaperChanger" `
|
--enable-plugin=anti-bloat `
|
||||||
--clean `
|
--include-package=requests `
|
||||||
|
--include-package=pystray `
|
||||||
|
--include-package=PIL `
|
||||||
|
--output-dir="$ScriptDir\dist" `
|
||||||
|
--output-filename=WallpaperChanger.exe `
|
||||||
|
--assume-yes-for-downloads `
|
||||||
"$ScriptDir\main.py"
|
"$ScriptDir\main.py"
|
||||||
|
|
||||||
if ($LASTEXITCODE -ne 0) { throw "PyInstaller 打包失败" }
|
if ($LASTEXITCODE -ne 0) { throw "Nuitka 打包失败" }
|
||||||
|
|
||||||
Write-Host ""
|
Write-Host ""
|
||||||
Write-Host "========================================" -ForegroundColor Green
|
Write-Host "========================================" -ForegroundColor Green
|
||||||
Write-Host " 编译完成!" -ForegroundColor Green
|
Write-Host " 编译完成!" -ForegroundColor Green
|
||||||
Write-Host " 输出: $ScriptDir\dist\WallpaperChanger\WallpaperChanger.exe" -ForegroundColor Green
|
Write-Host " 输出: $ScriptDir\dist\WallpaperChanger.dist\WallpaperChanger.exe" -ForegroundColor Green
|
||||||
Write-Host "========================================" -ForegroundColor Green
|
Write-Host "========================================" -ForegroundColor Green
|
||||||
|
|||||||
@@ -3,8 +3,12 @@ import os
|
|||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
|
||||||
|
def _is_frozen():
|
||||||
|
return getattr(sys, 'frozen', False) or '__compiled__' in sys.modules
|
||||||
|
|
||||||
|
|
||||||
def _get_app_dir():
|
def _get_app_dir():
|
||||||
if getattr(sys, 'frozen', False):
|
if _is_frozen():
|
||||||
return os.path.dirname(sys.executable)
|
return os.path.dirname(sys.executable)
|
||||||
return os.path.dirname(os.path.abspath(__file__))
|
return os.path.dirname(os.path.abspath(__file__))
|
||||||
|
|
||||||
|
|||||||
+6
-2
@@ -11,14 +11,18 @@ COMMAND_KEY = REG_KEY + r"\command"
|
|||||||
MENU_NAME = "刷新壁纸(&R)"
|
MENU_NAME = "刷新壁纸(&R)"
|
||||||
|
|
||||||
|
|
||||||
|
def _is_frozen() -> bool:
|
||||||
|
return getattr(sys, 'frozen', False) or '__compiled__' in sys.modules
|
||||||
|
|
||||||
|
|
||||||
def _get_app_dir() -> str:
|
def _get_app_dir() -> str:
|
||||||
if getattr(sys, 'frozen', False):
|
if _is_frozen():
|
||||||
return os.path.dirname(sys.executable)
|
return os.path.dirname(sys.executable)
|
||||||
return os.path.dirname(os.path.abspath(__file__))
|
return os.path.dirname(os.path.abspath(__file__))
|
||||||
|
|
||||||
|
|
||||||
def _get_command() -> str:
|
def _get_command() -> str:
|
||||||
if getattr(sys, 'frozen', False):
|
if _is_frozen():
|
||||||
return f'"{sys.executable}" --refresh'
|
return f'"{sys.executable}" --refresh'
|
||||||
else:
|
else:
|
||||||
base = os.path.dirname(sys.executable)
|
base = os.path.dirname(sys.executable)
|
||||||
|
|||||||
@@ -28,11 +28,13 @@ python main.py --refresh
|
|||||||
|
|
||||||
## 编译为 .exe
|
## 编译为 .exe
|
||||||
|
|
||||||
|
使用 Nuitka 打包:
|
||||||
|
|
||||||
```powershell
|
```powershell
|
||||||
powershell -ExecutionPolicy Bypass -File build.ps1
|
powershell -ExecutionPolicy Bypass -File build.ps1
|
||||||
```
|
```
|
||||||
|
|
||||||
产物:`dist/WallpaperChanger.exe`
|
产物:`dist/WallpaperChanger.dist/WallpaperChanger.exe`(独立目录模式,含 exe + 依赖库)
|
||||||
|
|
||||||
- 双击运行 → 启动托盘
|
- 双击运行 → 启动托盘
|
||||||
- `WallpaperChanger.exe --refresh` → 拉取并设置壁纸后退出(右键菜单用)
|
- `WallpaperChanger.exe --refresh` → 拉取并设置壁纸后退出(右键菜单用)
|
||||||
|
|||||||
Reference in New Issue
Block a user