27

I installed pyqt4 by using Homebrew. But when I import PyQt4 in python interpreter, It said that "No module named PyQt4". Can somebody help me with that?

Win83
  • 277
  • 1
  • 3
  • 3
  • 1
    Have you installed additional Python versions besides Apple's? If so, which ones? And which of them gets run when you just type `python` at the shell? (Generally, PyQt4 and similar packages on Homebrew can only work with Apple's Python or a Homebrew-built Python, not with, say, a Python.org or Enthought or ActiveState Python. Also, if you have two different Python 2.7 installations, it's very easy to install something to one site-packages, and then try to use it from the other, and not even realize you're doing anything wrong.) – abarnert Jun 07 '13 at 23:14
  • I think this link http://stackoverflow.com/questions/1961997/is-it-possible-to-add-pyqt4-pyside-packages-on-a-virtualenv-sandbox will help you. I do this, and it works. – aijogja Oct 08 '15 at 14:55

5 Answers5

23

After brew install pyqt, you can brew test pyqt which will use the python you have got in your PATH in oder to do the test (show a Qt window).

For non-brewed Python, you'll have to set your PYTHONPATH as brew info pyqt will tell.

Sometimes it is necessary to open a new shell or tap in order to use the freshly brewed binaries.

I frequently check these issues by printing the sys.path from inside of python: python -c "import sys; print(sys.path)" The $(brew --prefix)/lib/pythonX.Y/site-packages have to be in the sys.path in order to be able to import stuff. As said, for brewed python, this is default but for any other python, you will have to set the PYTHONPATH.

Eric
  • 3,773
  • 3
  • 29
  • 29
Samuel John
  • 1,362
  • 10
  • 12
14

You have to check which Python you are using. I had the same problem because the Python I was using was not the same one that brew was using. In your command line:

  1. which python
    output: /usr/bin/python
  2. which brew
    output: /usr/local/bin/brew     //so they are different
  3. cd /usr/local/lib/python2.7/site-packages
  4. ls   //you can see PyQt4 and sip are here
  5. Now you need to add usr/local/lib/python2.7/site-packages to your python path.
  6. open ~/.bash_profile   //you will open your bash_profile file in your editor
  7. Add 'export PYTHONPATH=/usr/local/lib/python2.7/site-packages:$PYTHONPATH' to your bash file and save it
  8. Close your terminal and restart it to reload the shell
  9. python
  10. import PyQt4    // it is ok now
Noumenon
  • 5,099
  • 4
  • 53
  • 73
Lily
  • 141
  • 1
  • 3
11

If you're using Anaconda to manage Python on your system, you can install it with:

$ conda install pyqt=4

Omit the =4 to install the most current version.

Answer from How to install PyQt4 in anaconda?

Jacob Beauchamp
  • 532
  • 5
  • 18
5

I solved the same problem for my own program by installing python3-pyqt4.

I'm not using Python 3 but it still helped.

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
Vasin Yuriy
  • 484
  • 5
  • 13
4

It is likely that you are running the python executable from /usr/bin (Apple version) instead of /usr/loca/bin (Brew version)

You can either

a) check your PATH variable

or

b) run brew doctor

or

c) run which python

to check if it is the case.

Anthony Kong
  • 37,791
  • 46
  • 172
  • 304