Files
WQ_GUI/scripts/rthook_add_dll_dirs.py

40 lines
1.2 KiB
Python
Raw Permalink Normal View History

2026-04-08 15:25:08 +08:00
import os
import sys
def _safe_add(path: str) -> None:
if not path or not os.path.isdir(path):
return
2026-05-11 17:38:29 +08:00
if hasattr(os, "add_dll_directory"):
try:
2026-04-08 15:25:08 +08:00
os.add_dll_directory(path)
2026-05-11 17:38:29 +08:00
return
except Exception:
pass
2026-04-08 15:25:08 +08:00
try:
os.environ["PATH"] = path + os.pathsep + os.environ.get("PATH", "")
except Exception:
pass
# PyInstaller onedir 解包目录
2026-04-08 15:25:08 +08:00
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