user3675085
user3675085

Reputation: 51

Pycharm import error with Pyqt4 and Pyqt5

I'm having some troubles with PyQt. I've installed PyQt4 and PyqQt5 through Homebrew, and I planned on using PyQt4 for a particular project I'm working on. However, I have some strange issues getting it to work.

In one module, we'll call it A, I'm using these imports:

from OpenGL.GL import *
from OpenGL.GLU import *
from PyQt4 import QtGui
from PyQt4.QtOpenGL import *

Now, PyQt4 is red underlined with 'unresolved reference to PyQt4'. However, this code runs. If I change it to PyQt5, there is no underline:

from OpenGL.GL import *
from OpenGL.GLU import *
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtOpenGL import *

But, I get this error when running:

Traceback (most recent call last):
  File "/Users/Jordan/PycharmProjects/SimpleCAD/QGLWindow.py", line 6, in <module>
    from PyQt5 import QtCore, QtGui, QtWidgets
ImportError: No module named PyQt5

Now, I have another window in my program, which I've taken care of in a different module, B. Those imports look like this:

import sys
from PyQt4 import QtGui

When I run this code, I get this error:

Traceback (most recent call last):
  File "/Users/Jordan/PycharmProjects/SimpleCAD/QTMainWindow.py", line 2, in <module>
    from PyQt4 import QtGui
ImportError: No module named 'PyQt4'

So, what's going on with my imports here? Why does PyQt4 work in one module, but not another? And why am I getting warnings for PyQt4 imports that work in one module, but none for the unworking PyQt5 imports? Thanks for any help in advance.

Upvotes: 4

Views: 13446

Answers (3)

phyatt
phyatt

Reputation: 19112

In PyCharm File > Invalidate Caches worked for me.

https://stackoverflow.com/a/11773462/999943

Hope that helps.

Upvotes: 4

arunjos007
arunjos007

Reputation: 4355

This is because you need to set your project directory as source root directory. Try this steps:

  1. Right click on your project directory
  2. Select option "Mark directory as" then click source root.

for more details check: Unresolved reference issue in PyCharm

Upvotes: 0

Julia Niewiejska
Julia Niewiejska

Reputation: 591

I had the same problem now with Python 3.5.1 and Pycharm 2016.1.2. What helped was the additional installation of the package python3-qtpy, so:

apt-get install python3-qtpy

Afterwards I wasn't getting any more unresolved references from PyQt4 in Pycharm.

Upvotes: 0

Related Questions