数值错误:命名空间 Gtk 不可用。

20

按照说明2.入门——Python GTK+ 3 教程 3.4 文档进行操作。

尝试。

In [6]: import gi
   ...: gi.require_version('Gtk', '3.0')
   ...: from gi.repository import Gtk

IT报告错误:

~/anaconda3/lib/python3.7/site-packages/gi/__init__.py in require_version(namespace, version)
    128     available_versions = repository.enumerate_versions(namespace)
    129     if not available_versions:
--> 130         raise ValueError('Namespace %s not available' % namespace)
    131
    132     if version not in available_versions:

ValueError: Namespace Gtk not available

请跟随如何在Ubuntu上安装GTK+ 3.0? - Ask Ubuntu并将Gtk安装到Ubuntu。

sudo apt-get install libgtk-3-dev

然而,它仍然报告相同的错误。

我该如何解决这个问题?

注意:

已尝试解决方案。

me@host:~:
$ sudo apt install python3-gi gobject-introspection gir1.2-gtk-3.0
Reading package lists... Done
Building dependency tree       
Reading state information... Done
gir1.2-gtk-3.0 is already the newest version (3.24.8-1ubuntu1).
gobject-introspection is already the newest version (1.60.1-1).
python3-gi is already the newest version (3.32.0-1).
0 upgraded, 0 newly installed, 0 to remove and 6 not upgraded.
me@host:~:
$ python
Python 3.7.3 (default, Mar 27 2019, 22:11:17) 
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import gi
>>> gi.require_version('Gtk', '3.0')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/me/anaconda3/lib/python3.7/site-packages/gi/__init__.py", line 130, in require_version
    raise ValueError('Namespace %s not available' % namespace)
ValueError: Namespace Gtk not available

安装其他组件

me@host:~:
$ sudo apt install -y gir1.2-gtk-3.0
Reading package lists... Done
Building dependency tree       
Reading state information... Done
gir1.2-gtk-3.0 is already the newest version (3.24.8-1ubuntu1).
0 upgraded, 0 newly installed, 0 to remove and 6 not upgraded.
me@host~:
$ ipython
Python 3.7.3 (default, Mar 27 2019, 22:11:17) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.4.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]:  import gi                                                                                                        

In [2]: gi.require_version('Gtk', '3.0')                                                                                  
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-2-3fc173a88e42> in <module>
----> 1 gi.require_version('Gtk', '3.0')

~/anaconda3/lib/python3.7/site-packages/gi/__init__.py in require_version(namespace, version)
    128     available_versions = repository.enumerate_versions(namespace)
    129     if not available_versions:
--> 130         raise ValueError('Namespace %s not available' % namespace)
    131 
    132     if version not in available_versions:

ValueError: Namespace Gtk not available

它没有起作用,

我的机器:

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 19.04
Release:    19.04
Codename:   disco

我在安装Dropbox到Ubuntu时遇到了问题

$ dropbox start
Starting Dropbox...
The Dropbox daemon is not installed!
Run "dropbox start -i" to install the daemon

运行"dropbox start -i"

$ dropbox start -i
Starting Dropbox...Traceback (most recent call last):
  File "/usr/bin/dropbox", line 1443, in start
    download()
  File "/usr/bin/dropbox", line 295, in download
    gi.require_version('Gdk', '3.0')
  File "/home/gaowei/anaconda3/lib/python3.7/site-packages/gi/__init__.py", line 130, in require_version
    raise ValueError('Namespace %s not available' % namespace)
ValueError: Namespace Gdk not available

请问您可以分享一下您从哪里获取了Dropbox客户端吗? - oz123
另外,您是否已安装GTK库?您正在使用标准桌面还是KDE? - oz123
我在Ubuntu上安装了标准的DropboxInstall - Dropbox,并使用标准桌面。@Oz123 - AbstProcDo
我认为您需要安装缺少的软件包。 - oz123
1
我遇到了这个错误,但是针对的是 gi.require_version('Gtk', '4.0')。我猜测 GTK 4.0 已经发布并正在广告宣传中,但 Ubuntu 20.04 只有 GTK 3.0。例如,请参考 https://askubuntu.com/questions/1304261/how-to-get-gtk4-in-ubuntu-20-04。 - nealmcb
1个回答

23

您需要安装gobject-introspectionpython3-gigir1.2-gtk-3.0。这些软件包包含了将GTK+库暴露给Python3的文件。

sudo apt install python3-gi gobject-introspection gir1.2-gtk-3.0

对于CENTOS (7)/OpenSUSE (15.1),您需要在dnf/yum/zypper中将软件包gir1.2-gtk-3.0替换为gtk3-devel - 感谢bgStack.

root@oz123:/home/oz123# python3
Python 3.6.8 (default, Jan 14 2019, 11:02:34) 
[GCC 8.0.1 20180414 (experimental) [trunk revision 259383]] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import gi
>>> gi.require_version('Gtk', '3.0')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/gi/__init__.py", line 130, in require_version
    raise ValueError('Namespace %s not available' % namespace)
ValueError: Namespace Gtk not available
>>> 
root@oz123:/home/oz123# apt install -y gir1.2-gtk-3.0
root@oz123:/home/oz123# python3
Python 3.6.8 (default, Jan 14 2019, 11:02:34) 
[GCC 8.0.1 20180414 (experimental) [trunk revision 259383]] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import gi
>>> gi.require_version('Gtk', '3.0')
>>> 

注意事项

在 Debian/Ubuntu 中,名为xyz-dev的软件包包含 C 头文件、pkg-config 文件和 autotools 宏。这些只有在您打算使用编译语言(如 C 和 C++)构建软件时才需要安装。它们会被安装到以下路径:

/usr/include/../xyz/header1.h  # headers
/usr/include/../xyz/header2.h
...
/usr/lib/x86_64-linux-gnu/pkgconfig/libxyz.pc  # pkgconfig files
...
/usr/share/aclocal/xyz.m4  # autotools macros

使用dpkg -L libgtk-3-dev命令查看此软件包在您的系统上安装的所有文件。

更新

请使用dpkg -r dropbox卸载dropbox,然后使用以下命令重新安装。

root@yeni2:/# dpkg -i dropbox_2019.02.14_amd64.deb 
Selecting previously unselected package dropbox.
(Reading database ... 126384 files and directories currently installed.)
Preparing to unpack dropbox_2019.02.14_amd64.deb ...
Unpacking dropbox (2019.02.14) ...
dpkg: dependency problems prevent configuration of dropbox:
 dropbox depends on libpango1.0-0 (>= 1.36.3); however:
  Package libpango1.0-0 is not installed.

dpkg: error processing package dropbox (--install):
 dependency problems - leaving unconfigured
Processing triggers for man-db (2.8.3-2) ...
Processing triggers for gnome-menus (3.13.3-11ubuntu1) ...
Processing triggers for desktop-file-utils (0.23-1ubuntu3) ...
Processing triggers for mime-support (3.60ubuntu1) ...
Processing triggers for hicolor-icon-theme (0.17-2) ...
Errors were encountered while processing:
 dropbox

现在,如果有缺失的依赖项,请使用以下命令进行修复:

root@yeni2:/# apt install -f
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Correcting dependencies... Done
The following additional packages will be installed:
  libpango1.0-0 libpangox-1.0-0
The following NEW packages will be installed:
  libpango1.0-0 libpangox-1.0-0
0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.
1 not fully installed or removed.
Need to get 45.0 kB of archives.
After this operation, 203 kB of additional disk space will be used.
Do you want to continue? [Y/n] 
Get:1 http://de.archive.ubuntu.com/ubuntu bionic/universe amd64 libpangox-1.0-0 amd64 0.0.2-5 [41.7 kB]
Get:2 http://de.archive.ubuntu.com/ubuntu bionic/universe amd64 libpango1.0-0 amd64 1.40.14-1 [3,332 B]
Fetched 45.0 kB in 0s (264 kB/s)    
debconf: delaying package configuration, since apt-utils is not installed
Selecting previously unselected package libpangox-1.0-0:amd64.
(Reading database ... 126411 files and directories currently installed.)
Preparing to unpack .../libpangox-1.0-0_0.0.2-5_amd64.deb ...
Unpacking libpangox-1.0-0:amd64 (0.0.2-5) ...
Selecting previously unselected package libpango1.0-0:amd64.
Preparing to unpack .../libpango1.0-0_1.40.14-1_amd64.deb ...
Unpacking libpango1.0-0:amd64 (1.40.14-1) ...
Setting up libpangox-1.0-0:amd64 (0.0.2-5) ...
Setting up libpango1.0-0:amd64 (1.40.14-1) ...
Setting up dropbox (2019.02.14) ...
Please restart all running instances of Nautilus, or you will experience problems. i.e. nautilus --quit
Dropbox installation successfully completed! You can start Dropbox from your applications menu.
Processing triggers for libc-bin (2.27-3ubuntu1) ...

非常感谢,但我在让它工作时遇到了一点问题,这是我的Ubuntu机器的问题吗?(更新了问题) - AbstProcDo
谢谢,我猜可能是我的Anaconda Python环境的问题。 - AbstProcDo
1
哦,天啊...是的。毫无疑问,它不知道 gi.repository...这与虚拟环境相同。venv 有一个标志 https://virtualenv.pypa.io/en/stable/reference/#cmdoption-system-site-packages,在 conda 中应该有类似的东西。 - oz123
1
谢谢。在Debian Bullseye(树莓派)上,我不需要安装gobject-introspection,只需要安装python3-gigir1.2-gtk-3.0即可。 - Apteryx

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