【问题标题】:Cannot import WebKit in python via gi.repository on Mac High Sierra无法通过 Mac High Sierra 上的 gi.repository 在 python 中导入 WebKit
【发布时间】:2018-09-22 00:07:09
【问题描述】:

在过去的几天里,我一直在尝试准备一个可以在 python 中使用 GTK 和 WebKit 的开发。我已经放弃了ubuntu,现在我只想先专注于mac。我已经安装了 pygobject3 并且可以加载 GTK 3.0。但我无法让 WebKit 工作。错误信息是这样的,

>>> import gi
>>> from gi.repository import WebKit
Traceback (most recent call last):
  File "<frozen importlib._bootstrap>", line 888, in _find_spec
AttributeError: 'DynamicImporter' object has no attribute 'find_spec'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.6/site-packages/gi/importer.py", line 127, in find_module
    'introspection typelib not found' % namespace)
ImportError: cannot import name WebKit, introspection typelib not found

我尝试过使用“WebKit2”、“WebKit3”等...

我还从webkit.org 下载并构建了 WebKit,但它什么也没做。

我很想获得一些指导以正确安装它,我可以上传您希望我运行的任何测试并在此处及时更新它。

非常感谢!

【问题讨论】:

    标签: python python-3.x macos webkit pygobject


    【解决方案1】:

    以下是目前导入WebKit的方式:

    import gi
    gi.require_version('WebKit2', '4.0')
    
    from gi.repository import WebKit2
    

    所需的 Debian/Ubuntu 软件包为:python3-gi gir1.2-webkit2-4.0 libwebkit2gtk-4.0-37

    这是一个呈现 Google 起始页的示例:

    import gi
    gi.require_version('Gtk', '3.0')
    gi.require_version('WebKit2', '4.0')
    
    from gi.repository import Gtk, WebKit2
    
    window = Gtk.Window()
    window.set_default_size(800, 600)
    window.connect("destroy", Gtk.main_quit)
    
    scrolled_window = Gtk.ScrolledWindow()
    webview = WebKit2.WebView()
    webview.load_uri("https://google.com")
    scrolled_window.add(webview)
    
    window.add(scrolled_window)
    window.show_all()
    Gtk.main()
    

    最后,可以在here 找到 PyGObject API 参考。

    【讨论】:

    • 解释一下“-1”票就好了
    • 我猜这是因为您回答了一个(可能有效的)ubuntu 解决方案来解决 Mac OS 问题
    • 好的。但是给定的 python 代码与操作系统无关。
    猜你喜欢
    • 2011-12-11
    • 2019-07-25
    • 2019-05-15
    • 1970-01-01
    • 1970-01-01
    • 2018-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多