You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PyCharm导入matplotlib.pyplot失败:PyQt5.QtGui无QApplication属性

Fix for AttributeError: module 'PyQt5.QtGui' has no attribute 'QApplication' when importing matplotlib.pyplot

Hey there, I’ve dealt with this exact headache after updating Python and setting up new conda packages—let me share how I got matplotlib working again!

What’s causing this?

In newer versions of PyQt5, the QApplication class was moved from the QtGui module to QtWidgets. But the inputhooks.py file (part of ipykernel) is still trying to call it from the old location, which throws that error when matplotlib tries to use the Qt backend.

Step-by-step fix:

  1. Locate the inputhooks.py file in your conda environment. The path will look something like this:

    {your_conda_env_path}/lib/pythonX.X/site-packages/ipykernel/inputhooks.py
    

    (Replace {your_conda_env_path} with the actual path to your environment, and X.X with your Python version like 3.9 or 3.10)

  2. Edit the file to fix the QApplication reference:

    • Find the line around 513 that says:
      app = QtGui.QApplication([" "])
      
    • Replace it with:
      app = QtWidgets.QApplication([" "])
      
  3. Check the imports at the top of the file. If you don’t see QtWidgets imported, add this line:

    from PyQt5 import QtWidgets
    

    Or update the existing PyQt5 import to include both modules if needed:

    from PyQt5 import QtGui, QtWidgets
    
  4. Search for other instances of QtGui.QApplication in the file—there might be more than one. Replace every occurrence with QtWidgets.QApplication.

  5. Save the file and restart your Python environment (close and reopen your console/Jupyter notebook), then try importing matplotlib.pyplot again.

This should resolve the attribute error because we’re now pointing to the correct location of QApplication in PyQt5.

内容的提问来源于stack exchange,提问作者Anna Semjén

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 03:58:45