导入 PyQt5 时 DLL 加载失败
- 2025-03-07 09:03:00
- admin 原创
- 74
问题描述:
我已经在 Windows 平台上安装了 PyQt5,并且出现了 importError:DLL 加载失败。
我已经使用命令安装了 pyqt5
pip3 install pyqt5
Successfully installed pyqt5-5.8.1
我的Python版本如下:
Python 3.5.2 |Anaconda custom (64-bit)| (default, Jul 5 2016, 11:41:13) [MSC v.1900 64 bit (AMD64)] on win32
导入错误如下:
from PyQt5.QtWidgets import QApplication
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: DLL load failed: The specified module could not be found.
感谢与问候
解决方案 1:
这是因为缺少Python3.dll
(stub dll,重新导出Python3x.dll
函数,以便一个版本的扩展可以适用于多个版本的python)。
如果你的 Python 发行版没有捆绑 python3.dll,你可以尝试 WinPython(https://winpython.github.io/)中的版本。
至少2017/04/01版本应该有它。
1)下载 WinPython('零'版本就足够了;必须与您的 Python 相同的“主版本” - 3.5/3.6 - 和“位数” - 32/64 - !!!)。
2) 解压到某个临时目录,取出 python3.dll 并将其粘贴到 python 目录中,位于 python3x.dll 旁边。
3)享受QT工作
解决方案 2:
我知道这个主题已经过时了,但是我在使用最新版本的 PyQT 5.11 时也遇到了这个问题,但我已通过以下方式将其降级到 5.9:
pip install PyQT5==5.9
并解决了这个问题。
解决方案 3:
如果您创建了虚拟环境,请检查是否python3.dll
已复制到Scripts
该虚拟环境的目录中。可能只有python35.dll
(或python36.dll
,等等,取决于 Python 版本)被复制,在这种情况下,您将收到错误。
解决方案 4:
答案有时很简单。在重新启动计算机之前,应用程序运行顺利,这让我很头疼。但是,我只是对自己说,也许 Windows 无法加载 DLL 模块?所以我再次重新启动计算机并再次运行。
完美运行。
解决方案 5:
就我而言,我使用的是 Windows 10 32 位和 Python 3.7.2。使用通过 pip 安装的 PyQt5 5.11 时出现此错误:
from PyQt5.QtWidgets import QApplication
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: DLL load failed: The specified module could not be found.
我注意到 5.11 版本没有 Qt DLL,所以我重新安装了早期版本,python -m pip uninstall PyQt5
并python -m pip install PyQt5==5.10
切勿使用 --no-cache-dir,因为它会在安装 whl 文件时产生断言错误:
assert building_is_possible
assertion Error
我最终安装了 5.10 并出现以下错误:
qt.qpa.plugin: Could not load the Qt platform plugin "windows" in "" even though it was found.
This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.
设置 QT_DEBUG_PLUGINS 甚至插件路径后:
set QT_DEBUG_PLUGINS=
set QT_PLUGIN_PATH=C:Python37-32Libsite-packagesPyQt5Qtplugins
我意识到 dll qwindows.dll 在其所在的正确路径中被搜索。
TL;DR:我使用 UAC 提升重新运行了我的 python Qt 脚本,你猜怎么着,它成功了!
解决方案 6:
我可以设法修复我的 conda 环境 (conda 4.6.11,python 3.6) 中的错误。我在这个问题上挣扎了很长时间,尝试了这里建议的所有修复方法。最后,我找到了解决办法!pip3 install
改为使用conda install
(我的 conda 似乎表现不佳),即使在 conda 环境中也是如此 (pip3,不仅仅是 pip,pip 对我来说不起作用)。
我是怎么发现的?我pip3 install PyQt5==5.9.1
,它加载得很好,但 DLL 加载失败的问题转移到了其他库,比如 PIL,所以我用 pip3 安装了它,然后我不得不用 pip3 重新安装每个库,现在它可以工作了。
解决方案 7:
在 Windows 10 中使用 Python 3.6,我按照以下步骤修复了此错误:
1)使用以下命令安装 PyQt5pip install pyqt5
2)正如微软网站所解释的,我修改了我的 Visual Studio 2017 Community 安装,启用了“Python 原生开发工具”
3)python3.dll
从复制C:Program Files (x86)Microsoft Visual StudioSharedPython36_64
到C:IntelPython3Libsite-packagesPyQt5
(检查你的目录)
解决方案 8:
我遇到了同样的问题。
问题是我的 PySide2 版本与 PyQt5 版本不同(PySide2:5.12,PyQt5:5.14)。我将 PySide2 升级到 5.14,它对我来说有效。(Python 3.6.5)
pip install PySide2==5.14
解决方案 9:
2022 年更新:
查看了有关 DLL 问题的评论后,没有一条是最近的,我碰巧弄清楚了为什么这个问题仍然存在。截至撰写本文时,PyQt5 的当前版本是 5.15.6。5.15.6、5.15.5 和 5.15.4 都存在 DLL 问题。不确定如何修复它,只能降级到 5.15.3。
实际上我从未升级过 PyQt5,因为我可能在其他地方遇到过类似的问题。但是当我安装 PyQtWebEngine 时,它会自动升级到最新的 PyQt5,并且至少需要 PyQt 版本 5.15.4(这会出现 DLL 错误)。我解决第二个问题的方法是将 PyQtWebEngine-5.15.5 降级到 PyQtWebEngine-5.15.3。
对于那些需要的人来说,总结一下:
PyQt5 可以工作:
pip 安装 PyQt5==5.15.3
PyQt5 和 PyQtWebEngine
pip 安装 PyQt5==5.15.3
pip 安装 PyQtWebEngine==5.15.3
解决方案 10:
我知道这个问题是关于 PyQT5 的,但这是搜索 PyQT6 中相同问题时最热门的帖子。因此,这个答案适用于任何在 PyQT6 中寻找解决方案的人。
我发现使用 PyQT6 版本 6.1.0 时会发生此问题。将 PyQT6 升级到最新版本(撰写此答案时为 6.3.0)可解决此问题。我注意到我的项目依赖于 PyQT6-Tools,这会将 PyQT6 的安装从 6.3.0 降级为 6.1.0。
我的解决方案是从 PyQT6-Tools 中删除项目依赖项并重新安装 PyQT6,这将再次安装最新版本。另一种方法是安装 PyQT6,然后安装 PyQT6-Tools,然后升级 PyQT6。我检查了 QTDesigner,它运行良好,但不确定 pyuic 是否运行良好。
解决方案 11:
...使用此参数--ignore-installed重新安装 PyQt5 python 包:
pip3 install PyQt5 --user --ignore-installed
解决方案 12:
我找到了另一种解决方法。
我使用虚拟环境是因为 PyCharm 已经为我创建了一个,而我不知道更好的方法。我已在那个虚拟环境中安装了 PyQt5。
我切换到不使用虚拟环境,并在全局 Python 目录中安装了 PyQt5。这样就解决了问题。
解决方案 13:
如果您安装了 PyQt5 附带的 Anaconda 版本,并且用 pip 安装版本覆盖了该软件包,也会发生这种情况。卸载 pip 安装版本解决了我的问题。
解决方案 14:
解决方案
关闭所有程序和编译器
打开 Anaconda Navigator 确保已经安装了 pyqt 和 qtpy 模块(qtawesome 可选)
从 Anaconda Navigator Home 启动 VS code,Jupyter 是你最喜欢的编辑器
运行你的程序!-
这不是一个永久性的解决方案,但它对我有用,希望它对你也有用@Miloslav Raus 的答案对我没用
解决方案 15:
如果你已经尝试了上述所有/大部分方法,但仍然无济于事。以下是解决方案。
首先,通过检查 PyQt5 路径排除虚拟环境问题。打开 Windows 终端,然后输入以下内容
python
import PyQt5
PyQt5.__file__
现在您已经看到了默认 Python 识别的 PyQt5 路径(之前__init__.py
),请在文件浏览器中检查它,您应该会找到 PyQt5 目录。没问题,DLL 导入错误不是由于这个问题引起的!
如果无法导入 DLL 问题仍然存在,那是因为您的 Windows 缺少更高的 Visual C++ 运行时环境。是的,适用于 Windows 的 PyQt5 是基于 Visual C++ 2015-2019 构建的。只需下载并安装该补丁到您的 Windows 并重新启动,问题就解决了。
解决方案 16:
更新 2020-06
在刚推出的 Windows 10 64 Pro 上,安装了全新的 Python 3.8 64,结果pipenv install pyside2
出现了该死的“DLL 加载失败”。经过双重验证的解决方案是低估了对Microsoft Visual C++ Runtime的依赖。万无一失的安装程序可在https://aka.ms/vs/16/release/vc_redist.x64.exe上找到。最快、最简单的安装方法:
winget install Microsoft.VCRedist.2015+.x64
解决方案 17:
You can try downloading 64bit Python Installer (Windows x86-64 executable installer) from here. I am using PyQt5==5.10.1
. Solve my problem.
解决方案 18:
In Windows 10, it's build-in python 3.7. And it seems too minimalistic.
Try uninstall it and install python3 from python.org
Then you should reinstall pyqt5 for new python.
解决方案 19:
Recently had this problem on a machine running on an old version of Python. The solution was to import every pyqt5 related modules before doing any other imports.
解决方案 20:
My PyQt5 was throwing this "module not found" error on 'from PyQt5 import QtGui'
When I reinstalled PyQt5, Pip threw an error traceback that started at:
*ValueError: Unable to find resource t64.exe in package
pip._vendor.distlib*
A quick check for t64.exe in Python\lib\site-packages\pip_vendor\distlib\ (the path given in the error message, but with dots) confirmed no t64.exe file.
Fortunately, I had a project distribution archived that had the t64.exe file, so I copied it over to my machine's C:\Program Files\Python directory and that handled the problem.
NOTE: I've been working on this since Windows crashed on startup yesterday and a VERY DICEY system recovery mangled my Python installation. I wiped out my entire Python installation and reinstalled it at least twice, running 'pip --upgrade' each time. One certainly wonders why the t64.exe file got dropped from the installation--and why a reinstall didn't restore it...
解决方案 21:
If anyone stumbles on this and is tempted to change their software...first try this simple fix which worked for me. Reboot first before you try anything else then try XSeg editing again.
解决方案 22:
I uninstall these packages and reinstall pyqt5 fix
pip uninstall pyqt5
pip uninstall pyqt5-sip
pip uninstall pyqt5-qt5
pip install pyqt5
解决方案 23:
My workaround to this problem was uninstalling and reinstalling PyQt.
use
pip list
to list all the installed packages.uninstall everything that related to the PyQt with
pip uninstall
(not just pyqt).Install PyQt again without using the cache like
pip install --no-cache-dir pyqt6
This worked for me.
解决方案 24:
我在使用 GNU Radio 的 GUI 时遇到了同样的问题。我的错误消息是导入 QtCore 时 DLL 加载失败:找不到指定的过程。我在其自己的环境中安装了 GNU Radio,但我尝试从 Windows 开始菜单启动它,所以我遇到了这个问题。有一次,我通过从环境中启动 GNU Radio 启动了相同的流程图,然后就没问题了。我手动启动了 Anaconda 提示符并激活了我安装 GNU Radio 的环境,错误就消失了。
解决方案 25:
如果这些选项都不起作用,请尝试这个(这正是我的情况)。它在 Pycharm 上肯定有效,但我不确定其他文本编辑器/IDE 是否有效。
Pycharm 在 External Libraries(Python version)\Libs 下有 2 个站点包。第一个是库根目录(用橙色表示),第二个是标准目录。对我来说,如果你查看 PyQt5 子目录,库根目录有许多子目录,而另一个只有 qsci 和一些 python 文件。如果你在第二个目录中打开 __ init __.py,find_qt 函数如下所示:
def find_qt():
import os, sys
qtcore_dll = '\\Qt5Core.dll'
dll_dir = os.path.dirname(sys.executable)
if not os.path.isfile(dll_dir + qtcore_dll):
path = os.environ['PATH']
dll_dir = os.path.dirname(__file__) + '\\Qt5\\bin'
if os.path.isfile(dll_dir + qtcore_dll):
path = dll_dir + ';' + path
os.environ['PATH'] = path
else:
for dll_dir in path.split(';'):
if os.path.isfile(dll_dir + qtcore_dll):
break
else:
return
try:
os.add_dll_directory(dll_dir)
except AttributeError:
pass
find_qt()
del find_qt
那里还有很多 DLL 文件。现在这里有个窍门:将所有这些文件复制粘贴到 Library Root 中。如果您需要 QtCore 以外的东西(例如 QtWidgets 或 QtGui),请编辑 init 中的 find_qt() 函数:
def find_qt(qt):
import os, sys
qt_dll = f'\\Qt5{qt}.dll' # New in Python 3.6, use string concatenation if using <3.6
dll_dir = os.path.dirname(sys.executable)
if not os.path.isfile(dll_dir + qt_dll):
path = os.environ['PATH']
dll_dir = os.path.dirname(__file__) + '\\Qt5\\bin'
if os.path.isfile(dll_dir + qt_dll):
path = dll_dir + ';' + path
os.environ['PATH'] = path
else:
for dll_dir in path.split(';'):
if os.path.isfile(dll_dir + qt_dll):
break
else:
return
try:
os.add_dll_directory(dll_dir)
except AttributeError:
pass
find_qt("Core")
find_qt("Gui")
find_qt("Widgets")
# And all the other stuff you need
del find_qt
解决方案 26:
@tom-mike 为我指明了正确的方向:在安装 PyQt(我的情况是 PyQt6)时,其他(子)包也会同时安装。但如果我们不为它们指定任何版本,pip 似乎会采用最新版本。就我的情况(安装PyQt6==6.5.3
)而言,我最终得到:
PyQt6
在版本中6.5.3
和PyQt6-qt6
在版本中6.6.0
。
为了解决这个问题,我通过以下方式卸载了 PyQt
pip uninstall PyQt6 PyQt6-qt6 PyQt6-sip
并将其重新PyQt6-qt6
安装为与 PyQt 相同的版本:
pip install pyqt6==6.5.3 pyqt6-qt6==6.5.3
解决方案 27:
我遇到了导致此问题的新情况:当我同时安装 PyQt6 和 PySide6 时。
以下是我的解决方案:
pip uninstall PyQt6
事情进展顺利。
解决方案 28:
对我来说,以下步骤有效!
pip uninstall PyQt6 -y
手动删除pyqt6相关文件夹
PythonPythonXYLibsite-packages
pip install PyQt6
这适用于 PyQt5 和 6
解决方案 29:
我尝试了这里的所有解决方案以及其他地方的一些解决方案,但是它们对我都不起作用。对我有用的解决方案是安装较新的 Python 版本。
解决方案 30:
不要使用pip来安装PyQt5!!
只需卸载所有 pip PyQt5 并使用conda install -c anaconda pyqt
扫码咨询,免费领取项目管理大礼包!