Repository: beeware/toga

Issue:
"ImportError: cannot import name WebKit" on Ubuntu 14.04
Installed toga via global `sudo pip install toga`. Then, tried to import it:

```
>>> import toga
ERROR:root:Could not find any typelib for WebKit
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/toga/__init__.py", line 86, in <module>
    from .platform.gtk.app import *
  File "/usr/local/lib/python2.7/dist-packages/toga/platform/gtk/app.py", line 7, in <module>
    from .window import Window
  File "/usr/local/lib/python2.7/dist-packages/toga/platform/gtk/window.py", line 6, in <module>
    from .command import SEPARATOR, SPACER, EXPANDING_SPACER
  File "/usr/local/lib/python2.7/dist-packages/toga/platform/gtk/command.py", line 1, in <module>
    from .widgets import Icon
  File "/usr/local/lib/python2.7/dist-packages/toga/platform/gtk/widgets/__init__.py", line 17, in <module>
    from .webview import WebView
  File "/usr/local/lib/python2.7/dist-packages/toga/platform/gtk/widgets/webview.py", line 3, in <module>
    from gi.repository import Gtk, WebKit
ImportError: cannot import name WebKit
```

Did a `sudo apt-get install python-webkit`, but still getting the same import error. I'm running Ubuntu under Crouton on a Chromebook, which doesn't always contain the full set of packages.

Since the application I aim to create (a GUI launcher for [KA Lite](https://github.com/learningequality/ka-lite/)) would rely on toga's awesome dedication to being pure Python and not needing any extra packages to be installed to work cross-platform, and since we wouldn't be needing the WebView, would it be possible to have it handle a lack of WebKit more gracefully, only erroring out if a WebView was actually used? Thanks!



Discussion:
I'd like to get to the bottom of exactly what isn't installed so we can document the requirement - but the request to make the install more resilient in the absence of WebKit would also be a good idea. Thanks for the suggestion!

Cool, happy to provide any info from my system that would be useful.

```
>>> gi.__version__
'3.12.0'
>>> import gtk; gtk.ver
(2, 28, 6)
```

Having installed `python-webkit` seems to allow me to `import webkit` directly, but doesn't affect the `gi`-based import. What other packages should I try installing? Am I perhaps using a newer/older version of one of the libraries?

It looks like the GI introspection data is provided by the gir1.2-webkit-3.0 package; however, it isn't clear to me why I've got that package but you don't. It looks like it might be an implied dependency of some of the ubuntu-desktop and ubuntu-release-manager tools. Are you running Ubuntu desktop or server? Is it possible you're missing some of the Ubuntu desktop tools in your install?

I can confirm that a `sudo apt-get install gir1.2-webkit-3.0` gets the toga-demo working properly.

I'm running Ubuntu desktop. It looks like the "ubuntu-desktop" package is not installed by default in Crouton (to keep it lightweight -- these things don't have much disk space on the SSD that comes with them).

Given that KA Lite gets installed in contexts where no internet is available (and hence packages can't be downloaded), a version that didn't depend on WebKit would be awesome. If you have suggestions for how to approach this, we could have someone from our team take a look at doing a PR.

Thanks!

The approach I'd suggest would be to wrap the `from gi.repository import WebKit` call in a try/catch block, and if `ImportError` is caught, set `WebKit = None`. Then, in the `_startup()` method, before trying to instantiate `WebKit.WebView()`, check to see if WebKit is None; if it is, raise a `RuntimeException` indicating that WebView isn't available. Django uses a similar approach to handle sites where PIL/Pillow aren't installed, and it works quite well; the module can be imported, but any attempt to use it at runtime will raise an exception.

Relevant code is here: https://github.com/pybee/toga/blob/master/toga/platform/gtk/widgets/webview.py


Resolving patch:
diff --git a/toga/platform/gtk/widgets/webview.py b/toga/platform/gtk/widgets/webview.py
--- a/toga/platform/gtk/widgets/webview.py
+++ b/toga/platform/gtk/widgets/webview.py
@@ -1,6 +1,13 @@
 from __future__ import print_function, absolute_import, division
 
-from gi.repository import Gtk, WebKit
+from gi.repository import Gtk
+
+# The following import sometimes fails; handle failure gracefully
+# (see https://github.com/pybee/toga/issues/26)
+try:
+    from gi.repository import WebKit
+except ImportError:
+    WebKit = None
 
 from .base import Widget
 
@@ -13,6 +20,12 @@ def __init__(self, url=None):
         self._webview = None
 
     def _startup(self):
+
+        if WebKit is None:
+            raise RuntimeError(
+                "Import 'from gi.repository import WebKit' failed;" +
+                " may need to install gir1.2-webkit-3.0 or similar.")
+
         self._impl = Gtk.ScrolledWindow()
         self._impl.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
 


Test patch: