精简打包之前
This commit is contained in:
33
rthook_add_dll_dir.py
Normal file
33
rthook_add_dll_dir.py
Normal file
@ -0,0 +1,33 @@
|
||||
"""
|
||||
PyInstaller Runtime Hook: Add DLL Directory
|
||||
|
||||
This hook ensures that the executable directory is added to the DLL search path,
|
||||
allowing packaged applications to load their bundled DLLs correctly.
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
try:
|
||||
# Get the base directory where the executable is located
|
||||
# For onefile: _MEIPASS (temporary extraction dir)
|
||||
# For onedir: executable parent directory
|
||||
base = Path(getattr(sys, "_MEIPASS", Path(sys.executable).parent))
|
||||
|
||||
# Python 3.8+: Explicitly add executable directory to DLL search path
|
||||
if hasattr(os, "add_dll_directory"):
|
||||
try:
|
||||
os.add_dll_directory(str(base))
|
||||
except Exception as e:
|
||||
# Silently ignore if adding fails (might already be added)
|
||||
pass
|
||||
|
||||
# Also add to PATH as fallback for older Python versions
|
||||
current_path = os.environ.get("PATH", "")
|
||||
if str(base) not in current_path:
|
||||
os.environ["PATH"] = str(base) + os.pathsep + current_path
|
||||
|
||||
except Exception as e:
|
||||
# Don't crash the application if hook fails
|
||||
pass
|
||||
Reference in New Issue
Block a user