Python 2.7无法导入PyQt4。

12

我已经从Ubuntu Maverick Meerkat (10.10)的Python 2.6升级到Python 2.7,但无法导入PyQt4。

austin@gerald:~$ python2.7
Python 2.7.0+ (r27:82500, Sep 15 2010, 18:04:55) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import PyQt4
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named PyQt4

我从Ubuntu的存储库中安装了Python 2.7(python2.7)和PyQt4(python-qt4)。我以前安装了PyQt,但是最近我安装了Python 2.7。

如果我尝试使用Python 2.6(因为它已经预先安装在Ubuntu系统中,命令简单地是python),它可以完美地工作。看一下:

austin@gerald:~$ python
Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import PyQt4

我知道问题所在:PyQt4模块可以被Python 2.6访问,但无法被Python 2.7访问。我的问题是,如何使Python 2.7能够找到PyQt4模块?求助!

注意:如果这个问题不应该在Super User上讨论,请见谅,我只是在编程时遇到了这个问题。

6个回答

9

4
在Ubuntu系统中,您可以通过apt-get安装它。
apt-get install python-qt4

将此标记为答案,因为即使在最古老的支持Ubuntu版本(12.04 LTS atm),python软件包现在也是2.7版本。谢谢! - Austin Adams
3
注意:如果您正在使用虚拟环境,则需要从 <env>/lib/python2.7 中删除 no-global-site-packages.txt 文件,才能从全局 Python site-packages 导入 PyQt4。 - Cerin

4

已测试OSX 10.8.2:

sudo pip install SIP

出现IOError错误:[Errno 2]没有这个文件或目录:'/tmp/pip-build/SIP/setup.py'

那就没问题了:

cd /tmp/pip-build/SIP
sudo python configure.py
make
sudo make install

从以下网址下载PyQt:http://sourceforge.net/projects/pyqt/files/PyQt4/PyQt-4.9.6/PyQt-mac-gpl-4.9.6.tar.gz,然后解压缩。
cd PyQt-mac-gpl-4.9.6
sudo python configure.py
make

完成后需要一些时间:

sudo make install    

享受它 :)


运行make命令时出现错误:无法打开输出文件'main.o':'打开输出文件'main.o'时出错'。 - Lango

2

1

您应该在Python 2.7环境中安装PyQt4。要执行此操作,请运行

easy_install2.7 PyQt4

如果未安装 easy_install,则需要先下载并运行安装它。
python2.7 setup.py install

1
不好意思,我想说一句话,为什么不用pip呢?你没听说过,“easy_install已死,pip万岁”吗? :) - mouad
2
那不起作用。当我运行“easy_install-2.7 PyQt4”时,它只是说“Reading http://pypi.python.org/simple/PyQt4/ Couldn't find index page for 'PyQt4' (maybe misspelled?) Scanning index of all packages (this may take a while) Reading http://pypi.python.org/simple/ No local packages or download links found for PyQt4 error: Could not find suitable distribution for Requirement.parse('PyQt4')" :( 我该如何使用pip? - Austin Adams

1
请先安装SIP模块,这是Python和C/C++库工具以及PyQt4库。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接