fix(main): 解决 Windows Free-Threading 环境下 DLL 加载问题

- 添加 shutil 模块导入用于查找可执行文件
- 为 Windows 环境手动注入 DLL 搜索路径
- 使用 shutil.which 查找 psql 可执行文件位置
- 将 psql 目录添加到 DLL 搜索路径以避免运行失败
- 针对 Python 3.8+ 的 DLL 加载行为变化进行适配
This commit is contained in:
developer-wlj
2026-09-02 20:07:56 +08:00
parent 089b007a00
commit 39d4fc12d0
+8
View File
@@ -1,6 +1,14 @@
import os
import shutil
import sys
# Windows 环境下手动注入 DLL 搜索路径
# Python 3.8+ 不再从环境变量 PATH 中自动加载 DLL。由于 psycopg 在 Free-Threading 版本中强制使用 C 扩展,
# 为避免因缺少底层 C 库导致运行失败,必须手动将 psql 所在目录加入 DLL 搜索路径。
if os.name == "nt":
psql_exe = shutil.which("psql")
if psql_exe:
os.add_dll_directory(os.path.dirname(psql_exe))
def _prepare_direct_execution_import_path() -> None:
"""