Python笔记:在环境变量中增加了dll加载路径,python提示DLL加载失败

发布于:2025-05-14 ⋅ 阅读:(21) ⋅ 点赞:(0)

在环境变量中增加了dll加载路径,python提示DLL加载失败

1.问题描述

from PySide2 import QtCore
提示如下错误
ImportError: DLL load failed while importing QtCore: 找不到指定的模块。

2.问题原因

在Python3.8文档中的What’s New In Python 3.8找到如下说明:

•DLL dependencies for extension modules and DLLs loaded with ctypes on Windows are now resolved more securely. Only the system paths, the directory containing the DLL or PYD file, and directories added with add_dll_directory() are searched for load-time dependencies. Specifically, PATH and the current working directory are no longer used, and modifications to these will no longer have any effect on normal DLL resolution. If your application relies on these mechanisms, you should check for add_dll_directory() and if it exists, use it to add your DLLs directory while loading your library. Note that Windows 7 users will need to ensure that Windows Update KB2533623 has been installed (this is also verified by the installer). (Contributed by Steve Dower in bpo-36085.)

在Python 3.8之前,Windows上的Python解释器会在环境变量PATH中搜索DLL文件。然而,从Python 3.8开始,这一行为发生了变化,Python解释器不再直接从PATH中导入DLL,而是采用了一种更严格的搜索策略,以提高安全性和可维护性。

3.8及以上的搜索路径为:

  • 系统目录
  • 当前dll或PYD文件所在目录
  • 通过os.add_dll_directory增加的目录

特别注意下,PATH与当前工作目录不再搜索。

3. 解决方案

  • 将dll放到系统目录下
  • 将dll放到当前dll或PYD文件所在目录
  • 通过os.add_dll_directory增加的dll的加载目录

网站公告

今日签到

点亮在社区的每一天
去签到