Files
cutThenThink/pyi_hooks/pyi_rth_ignore_torch.py

48 lines
1.3 KiB
Python
Raw Permalink Normal View History

"""
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())