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