4

I have been trying to prepare a development where I can use GTK and WebKit in python for the past few days. I have given up on ubuntu and now I just want to focus on mac first. I have installed the pygobject3 and I can load the GTK 3.0. But I can't get the WebKit working. The error message is this,

>>> 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

I have tried with "WebKit2", "WebKit3" etc...

I have also downloaded and build the WebKit from webkit.org,and it did nothing.

I would love to have some guidance over all to properly install it, I can upload any test you wish me to run and promptly update it here.

Thank you very much!

Sen
  • 43
  • 8

1 Answers1

-2

The following is the current way of importing WebKit:

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

from gi.repository import WebKit2

The required Debian/Ubuntu packages are: python3-gi gir1.2-webkit2-4.0 libwebkit2gtk-4.0-37

Here is an example that renders Google starting page:

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()

Finally, the PyGObject API Reference can be found here.

Tobias Sette
  • 185
  • 12