Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing matplotlib.pyplot fails in PyCharm due to AttributeError: module 'PyQt5.QtGui' has no attribute 'QApplication'

I wanted to change my python compiler to "newer" one (within one project) and use some additional packages with conda. After the installation all my packages worked fine and I could use the console, however matplotlib.pyplot fails to import since then. And throws the following error: app = QtGui.QApplication([" "]) AttributeError: module 'PyQt5.QtGui' has no attribute 'QApplication'

After doing some research on this I found this one: link It is suggesting to change inputhooks.py (on line 513 and change GUI_QT:enable_qt4 to GUI_QT: enable_qt5 4 to 5) As it is a company machine, I can't have admin rights to overwrite things within PyCharm.

Can you recommend a better solution to avoid this?

Why is this popping up? ErrorLogScreenshot

Also I tried to install the newer version of pyqt but that didn't fix the problem.

In the same time Within pydev the interactiveshell.py fails as well. with the following error message:self.showtraceback(running_compiled_code=True) TypeError: showtraceback() got an unexpected keyword argument 'running_compiled_code' I understand it gets an argument which it is not expecting. I did some research on this one as well and some could fix it by deleting a stale a corresponding .pyc file (I couldn't find one at the same location as the initial file only interactiveshell.py)

Below this blog regarding pydev the conversation never went further https://github.com/ipython/ipython/issues/10687

Is there a way to fix it? I am kind of new to Python and don't quite understand the heart of it at this depth so any help is appreciated.

Thanks, Anna

like image 589
Anna Semjén Avatar asked May 07 '18 20:05

Anna Semjén


1 Answers

For anyone having the same issue, I have solved this problem by switching to qt and pyqt 4. To do so, follow the following procedure:

conda remove qt   
conda install qt=4
conda install -c anaconda pyqt=4.11.4
conda install matplotlib --no-update-dependencies

If you are not using a virtual environment, you might also have to remove anaconda navigator before installing new packages:

conda uninstall anaconda-navigator

At the beginning of your program, switch matplotlib backend:

import matplotlib.pyplot as plt
plt.switch_backend('Qt4Agg')

Inspired by this solution.

like image 187
Sep Avatar answered Nov 15 '22 01:11

Sep