11

I am using Eclipse 3.7.1 with the latest PyDev add-in for Python coding. I am using PyQt4. At the top of my file I have:

from PyQt4.QtCore import *
from PyQt4.QtGui import *

In addition, I have the PyQt4 tree included in the Project Explorer listing. However, eclipse still thinks the names like QMainWindow are undefined. The code runs fine. How may I get eclipse to recognize those names.

Thanks

Kevin Buchs
  • 2,520
  • 4
  • 36
  • 55

2 Answers2

16

PyQt is actually a wrapping of C++ Qt libraries. So they are not .py files and PyDev can't analyze them to get what is in them. You need to add PyQt4 in the Forced Builtins tab, so that PyDev can use a Python shell to "look into" those libraries and know what is in them. That will also give you code-completion for PyQt.

Apart from that, it is usually not a good practice to use from foo import *. You'll be importing everything inside your namespace and you wouldn't know which is coming from where. Moreover you might have name clashes that mask each other. Though it is unlikely with PyQt, still I'd suggest you get used to from PyQt4 import QtGui, QtCore and reference classes like QtGui.QMainWindow.

Avaris
  • 35,883
  • 7
  • 81
  • 72
  • 1
    Thanks, Avaris. You solution worked well and makes sense. I also appreciate the reminder about good programming style in Python. It is often the case that those who advocate good style have experienced the drawbacks of not following it, i.e., they know the pain. – Kevin Buchs Nov 14 '11 at 17:36
0

it happens sometimes that PyDev lose its mind... If restarting Eclipse don't do the trick, consider doing this: PyDevPreferencs

Click on the "Apply" button and select your python interpreter. That should force Eclipse to recover existing libraries.

Jeannot
  • 1,165
  • 7
  • 18
  • Thanks for the ideas, however, it has been persistent across many restarts. A coworker has the same problem. I did set up the interpreter anew, and I even added the site-packages\PyQt4 as a separate folder in the bottom pane you pictured. – Kevin Buchs Nov 10 '11 at 18:38