如何在Python2.7中安装GTK?

8

我正在使用Debian 6.04和Python 2.7
我编译了Python 2.7(./configure,make,make install)
在控制台中:

>python2.7  
Python 2.7.3 (default, Jul 28 2012, 16:54:06) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import gtk
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named gtk

如何在Python 2.7中安装gtk?

在Python 2.6中:

tiger@debian:~$ python
Python 2.6.6 (r266:84292, Dec 27 2010, 00:02:40) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import gtk
>>> import pygtk
>>> import gobject

5
请不要在您的系统上执行 sudo pip install(一些答案建议这样做)。这将绕过主要的软件包管理系统,升级时可能会带来麻烦。请使用 virtualenv 或使用标准 Debian 软件源中提供的 pygtk 版本并通过 apt-get 安装它。为什么要编译 Python?Debian 中没有打包2.7版本吗? - Noufal Ibrahim
3个回答

5

尝试使用pip/python-pip/easy_install进行安装。 在Fedora上,我是这样安装它的:

sudo pip install PyGTK

55
感谢您。因为某些原因,我遇到了这个错误:“仅支持在Windows上使用distutils构建PyGTK。” - SSH This
你也可以只使用 pygtk。它不区分大小写。 - smac89

3

我通常会使用以下命令基于Python 2.7 创建虚拟环境:

$ virtualenv -p /usr/bin/python2.7 .

然后在虚拟环境中安装您的软件包。

不过,pygtk 是一个特殊的例子:它只能在 Windows 平台上通过 PyPI 进行安装,因为它的一些依赖项(对于 Posix 版本)在 PyPI 上不可用。

在 Debian 中,使用以下命令在系统范围内安装 PyGTK 包:

$ sudo pip install PyGTK

1
你曾经找出答案了吗? - user230910

3

对于Windows系统:

pip install PyGTK

针对 MacOS:

brew install pygtk
  • 请注意,使用brew安装的Python2时可以直接使用此方法(对我来说,路径是/usr/local/Cellar/python\@2/2.7.15/bin/python2.7)。如果您使用其他方式安装Python,请确保将PYTHONPATH附加到环境变量中。

对于Ubuntu:(与系统安装的Python2一起使用)

sudo apt-get install python-gtk2-dev

Conda:

conda install -c ostrokach gtk
  • 如果您在conda环境中运行Python2。

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