import os import sys def _safe_add(path: str) -> None: if not path or not os.path.isdir(path): return if hasattr(os, "add_dll_directory"): try: os.add_dll_directory(path) return except Exception: pass try: os.environ["PATH"] = path + os.pathsep + os.environ.get("PATH", "") except Exception: pass # PyInstaller onedir 解包目录 base = getattr(sys, "_MEIPASS", None) if base: _safe_add(base) _safe_add(os.path.join(base, "lib-dynload")) _safe_add(os.path.join(base, "DLLs")) # osgeo 子目录包含 gdal.dll / geos.dll / proj_9.dll 等 GIS 核心库 osgeo_dir = os.path.join(base, "osgeo") _safe_add(osgeo_dir) # ★ 关键:在 PyQt5 之前预加载 osgeo 的所有 DLL # 避免 Qt5 DLL 加载后 osgeo DLL 加载时发生符号冲突 if os.path.isdir(osgeo_dir): import ctypes as _ct for _f in os.listdir(osgeo_dir): if _f.lower().endswith('.dll'): _dll_path = os.path.join(osgeo_dir, _f) try: _ct.CDLL(_dll_path) except Exception: pass