You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

PyCharm导入PyQt5.QtWidgets时DLL加载失败问题求助

问题描述

运行以下PyQt5代码时出现DLL加载失败错误:

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout
from PyQt5.QtCore import QTimer, QTime, Qt

class DigitalClock(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        pass

if __name__ == '__main__':
    app = QApplication(sys.argv)
    clock = DigitalClock()
    clock.show()
    sys.exit(app.exec_())

终端报错信息:

Traceback (most recent call last):
  File "C:\Users\nnils\PycharmProjects\DigitalClock\main.py", line 2, in <module>
    from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout
ImportError: DLL load failed while importing QtWidgets: Die angegebene Prozedur wurde nicht gefunden.

Process finished with exit code 1

已尝试的解决方法:卸载重装PyQt5、完全重装PyCharm和Python解释器、更换更早版本的解释器,均无效。

可行解决方案
  • 调整系统Path优先级
    系统中可能存在其他版本的Qt DLL(如Qt Creator或其他软件自带的),与PyQt5依赖版本冲突。打开系统环境变量的Path,将Python环境下的Lib\site-packages\PyQt5\Qt\bin路径移至最顶部,确保PyQt5的DLL被优先加载。

  • 本地安装PyQt5官方轮子
    先卸载现有PyQt5相关包:

    pip uninstall -y PyQt5 PyQt5-sip
    

    从PyPI下载与你的Python版本、系统架构匹配的PyQt5轮子文件(.whl格式),再本地安装:

    pip install 本地轮子文件路径
    
  • 排查并修复DLL依赖
    用Dependency Walker工具打开PyQt5\QtWidgets.pyd文件,查看缺失的具体DLL或函数。若为系统级DLL缺失,安装对应版本的VC++ Redistributable运行库(PyQt5编译依赖的版本,如VS2019或VS2022)。

  • 创建全新虚拟环境
    避免现有环境的依赖冲突,新建虚拟环境后重装PyQt5:

    python -m venv venv
    venv\Scripts\activate
    pip install PyQt5
    

    在PyCharm中选择该虚拟环境作为解释器,重新运行代码。

内容的提问来源于stack exchange,提问作者Nils D.

火山引擎 最新活动