Can't import PyQt4 modules


  • Anonymous

    I just installed pydev, v1.6.3 into eclipe (Galileo) running on a Windows XP machine. After configuring pydev to use Python 2.6 as the interpreter, I attempted to create a project that would import PyQt4 modules to build a simple app. Unfortunately, pydev isn't importing these correctly. The import statement is

    from PyQt4 import QtGui, QtCore

    This statement is not being flagged as an error by eclipse. But, when I attempt to create a class

    class MainWindow(QtGui.QMainWindow):
       def __init__(self, parent=None):
          <more code follows>

    The class line is marked with a red x. The pop-up for the error states that QMainWindow is an undefined variable. This happens at several other lines that reference other QtGui and QtCore classes. I know these are, in fact, valid because I can run the program directly from python in a command window.

    The system PYTHONPATH defined in Eclipse does include the Lib/site-packages folder in which PyQt4 resides. Other modules in site-packages can be used without errors so I'm pretty sure the path is being set correctly.

    Any ideas as to what is wrong and what I can do to fix it?

  • Fabio Zadrozny
    Fabio Zadrozny

    Do you have some error in your error log? Pydev should spawn a shell to analyze the completions or PyQt4… you could try adding 'PyQt4' to the forced builtins. See



  • Diogo

    Im having a similar problem with PyDev PyQt4. When i try to run my script (from PyQt4 import QtGui) i get the following error: Traceback (most recent call last):
      File "/Users/henriques/EclipseProjects/python/HelloWorld/src/", line 8, in <module>
        from PyQt4 import QtGui
    ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/PyQt4/, 2): Symbol not found: _PyCapsule_Import
      Referenced from: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/PyQt4/
      Expected in: flat namespace
    in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/PyQt4/

    The import is ok but any functions from the QtGui namespace gives an error (symbol not found). I have tried the following things: run directly from python shell (it works); add /lib and /lib/site-packages (where pyqt is), add only /lib/site-packages, add /lib/site-packages and /lib/site-packages/PyQt4/, add only /lib/site-packages/PyQt4/.
    Im currently using /lib /lib/site-packages. Other imports like scipy works fine. I have also tried to enable/disable code analysis.
    Im currently using python 2.7 with PyQt4 (latest version) with Eclipse Hellios and up to date PyDev. (Snow Leopard x86_64). I have build my libraries with the i386 arch and they work fine with python shell (terminal).

    Thanks in advance.

  • Fabio Zadrozny
    Fabio Zadrozny

    In this case, the problem doesn't seem to be the PYTHONPATH, but some other variable (not sure what variables pyqt  needs, but I'd go for PATH and LIBPATH). Try seeing the differences from what you have inside eclipse and from your shell (note that /lib/site-packages/PyQt4 should NOT be added to your PYTHONPATH, only /lib/site-packages/, but things may be different for the other variables).

    Usually if you launch eclipse from the same shell where you have it working with the python shell, those should be correct.



  • Diogo

    Dude, sorry for taking this long to reply but i`ve been very busy these last few days. You were correct. I feel so stupid right now, the system comes with an ApplePythonish version whilst i had an "official" python installed. Terminal was using the official python while pydev was using Apple`s python. I made a simbolic link from my python to the /usr/bin  and it worked perfectly. Thanks! Just so that other ppl using Snow Leopard and python can get it right with PyQt4 here is what i did.
    1-Make sure eclipse is using the correct python terminal (if you click auto-configure it will go to the apple`s pyton, the default location for the user installed python is at /Library/Frameworks/Python.Frameworks/Version/X.X/bin)
    2-Set eclipse PYTHONPATH to equal terminal`s pythonpath (i go-> import sys then print sys.path within the python shell to view pythonpath idk if there is another way)
    3-Make sure the installed libraries are all working within python shell
    4-(dont know if it works w/o this but i changed it anyways) Inside the environment tab set the value of path to terminal`s path (echo $PATH inside terminal)
    This is it thanks again!