PyObjC WebKit Framework Bindings
PyObjC-framework-WebKit provides Python wrappers for Apple's WebKit and JavaScriptCore frameworks on macOS, enabling Python applications to embed web content and execute JavaScript. It is part of the larger PyObjC project, which acts as a bridge between Python and Objective-C. The library is actively maintained with regular updates to support new macOS SDKs and Python versions, typically following a release cadence aligned with major macOS and Python releases.
Warnings
- breaking PyObjC v12.0 dropped support for Python 3.9, and v11.0 dropped support for Python 3.8. Ensure your Python environment is 3.10 or newer for PyObjC 12.x.
- breaking PyObjC v10.3 introduced a change where `__init__` methods in Python subclasses of Objective-C classes would not be called if a user-defined `__new__` was not present. This broke several projects. While partially reverted in v10.3.1 to allow `__init__` when a user *does* implement `__new__`, code relying on PyObjC's auto-generated `__new__` still cannot use `__init__` in the traditional Python sense.
- gotcha Building PyObjC (including framework bindings) from source requires Xcode Command Line Tools to be installed, which provides the necessary compilers and SDKs. Without them, installation via `pip install --no-binary :all: pyobjc-framework-webkit` will fail.
- gotcha Conflicting `AppKit` packages can cause `ImportError`. If you encounter 'No module named AppKit' after installing PyObjC, check if another non-PyObjC `AppKit` package is installed.
- gotcha Due to the underlying Objective-C runtime, it's generally recommended to avoid subclassing `NSString` or `NSMutableString` in Python, as these classes have special handling in PyObjC and direct subclassing can lead to crashes.
Install
-
pip install pyobjc-framework-webkit
Imports
- WebKit
import WebKit
- AppKit
import AppKit
- Foundation
import Foundation
Quickstart
import AppKit
import Foundation
import WebKit
class AppDelegate(AppKit.NSObject):
def applicationDidFinishLaunching_(self, notification):
rect = Foundation.NSMakeRect(0, 0, 800, 600)
self.window = AppKit.NSWindow.alloc().initWithContentRect_styleMask_backing_defer_(
rect, AppKit.NSWindowStyleMaskTitled | AppKit.NSWindowStyleMaskClosable | AppKit.NSWindowStyleMaskResizable, AppKit.NSBackingStoreBuffered, False
)
self.window.setTitle_("PyObjC WebKit Demo")
self.webView = WebKit.WKWebView.alloc().initWithFrame_(rect)
self.window.contentView().addSubview_(self.webView)
url = Foundation.NSURL.URLWithString_("https://www.apple.com/" if not Foundation.NSBundle.mainBundle().bundleIdentifier() else "https://www.google.com")
request = Foundation.NSURLRequest.requestWithURL_(url)
self.webView.loadRequest_(request)
self.window.makeKeyAndOrderFront_(None)
def applicationShouldTerminateAfterLastWindowClosed_(self, sender):
return True
if __name__ == "__main__":
app = AppKit.NSApplication.sharedApplication()
delegate = AppDelegate.alloc().init()
app.setDelegate_(delegate)
app.run()