4

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.

user3675085
  • 51
  • 1
  • 1
  • 3
  • First question: How many Python installations do you have? Just the pre-installed Apple one, that one plus a Homebrew one, both of those plus a python.org installer, …? Because my first guess is that you've installed PyQt4 into one Python installation and are trying to use it from another one that you haven't installed it into. (And that PyCharm is using one to do its code completion/etc. features, and the other one to run your scripts.) – abarnert Jul 25 '14 at 22:14
  • I only have two Hombrew installs: 2.7 and 3.4. I got rid of all others, and all of my downloaded modules use the Homebrew installations. So, your comment helped me figure out my issue though. One module was using 3.4, and the other 2.7. When I edited the Pycharm configs, I incorrectly assumed it apply to all modules in a project. Thanks! Though, the red underlines still persist. Out of curiosity, what is causing that? Works without an issue, though. – user3675085 Jul 25 '14 at 22:18
  • My first guess would be the one I mentioned above: is PyCharm configured to use the wrong Python installation for code completion? – abarnert Jul 25 '14 at 23:05
  • 2
    Meanwhile, do you want to close/delete this question, or do you think it'll be useful for people with similar problems (in which case you should write and accept your own answer, unless you want me to do it)? – abarnert Jul 25 '14 at 23:06
  • Possible duplicate of [PyCharm can't resolve references to PyQT5 modules](http://stackoverflow.com/questions/43824942/pycharm-cant-resolve-references-to-pyqt5-modules) – Amorphous May 08 '17 at 17:38

3 Answers3

4

In PyCharm File > Invalidate Caches worked for me.

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

Hope that helps.

phyatt
  • 18,472
  • 5
  • 61
  • 80
0

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

Community
  • 1
  • 1
arunjos007
  • 4,105
  • 1
  • 28
  • 43
0

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.

Julia Niewiejska
  • 571
  • 3
  • 11