fix: 修复Python 3.13环境下PyInstaller构建崩溃问题
问题: - PyInstaller在分析阶段尝试导入torch/transformers等ML库 - 这些库与Python 3.13不兼容,导致"Windows fatal exception: access violation" - 构建过程无法完成,无法生成exe文件 解决方案: 1. 添加pyi_hooks/pyi_rth_ignore_torch.py运行时hook - 在PyInstaller分析阶段阻止torch等模块的导入 - 这些模块将在运行时动态安装 2. 添加pyi_hooks/hook-exclude-ml.py和hook-paddleocr.py - 明确告诉PyInstaller不收集这些ML库 3. 更新build.bat和build.sh - 使用--runtime-hook和--additional-hooks-dir - 添加--exclude-module参数排除所有ML库 测试: - 验证所有hook文件语法正确 - 验证项目核心功能可以在不导入torch的情况下正常工作 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
47
pyi_hooks/pyi_rth_ignore_torch.py
Normal file
47
pyi_hooks/pyi_rth_ignore_torch.py
Normal file
@@ -0,0 +1,47 @@
|
||||
"""
|
||||
PyInstaller runtime hook to prevent torch import during analysis
|
||||
|
||||
This hook runs before PyInstaller's module analysis phase.
|
||||
It blocks problematic modules from being imported during the build process.
|
||||
"""
|
||||
|
||||
import sys
|
||||
|
||||
# List of modules to block during PyInstaller analysis
|
||||
BLOCKED_MODULES = [
|
||||
'torch',
|
||||
'transformers',
|
||||
'tensorflow',
|
||||
'onnx',
|
||||
'onnxruntime',
|
||||
'sentencepiece',
|
||||
'tokenizers',
|
||||
'diffusers',
|
||||
'accelerate',
|
||||
'datasets',
|
||||
'huggingface_hub',
|
||||
'safetensors',
|
||||
]
|
||||
|
||||
# Override the import mechanism to block these modules
|
||||
class BlockModuleImport:
|
||||
"""Meta path importer to block specific modules during PyInstaller build"""
|
||||
|
||||
def find_module(self, fullname, path=None):
|
||||
if fullname in BLOCKED_MODULES or any(
|
||||
fullname.startswith(m + '.') for m in BLOCKED_MODULES
|
||||
):
|
||||
# Return self to handle the import (will raise ImportError)
|
||||
return self
|
||||
return None
|
||||
|
||||
def load_module(self, fullname):
|
||||
raise ImportError(
|
||||
f"Module '{fullname}' is excluded from PyInstaller build. "
|
||||
f"It will be installed at runtime if needed."
|
||||
)
|
||||
|
||||
# Install the blocker at the start of PyInstaller analysis
|
||||
if '_MEIPASS' not in sys.__dict__:
|
||||
# Only during build time (not when running the frozen app)
|
||||
sys.meta_path.insert(0, BlockModuleImport())
|
||||
Reference in New Issue
Block a user