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?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
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:
which python
output: /usr/bin/python
which brew
output: /usr/local/bin/brew //so they are different
cd /usr/local/lib/python2.7/site-packages
ls
//you can see PyQt4 and sip are hereusr/local/lib/python2.7/site-packages
to your python path.open ~/.bash_profile
//you will open your bash_profile file in your editor'export PYTHONPATH=/usr/local/lib/python2.7/site-packages:$PYTHONPATH'
to your bash file and save itpython
import PyQt4
// it is ok nowIt 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.
After
brew install pyqt
, you canbrew 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 thesys.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 thePYTHONPATH
.I solved the same problem for my own program by installing
python3-pyqt4
.I'm not using Python 3 but it still helped.
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?