PyObjC Framework - Cocoa
PyObjC-framework-Cocoa provides Python wrappers for the core Cocoa frameworks on macOS, including CoreFoundation, Foundation, and AppKit. It acts as a bidirectional bridge, enabling Python developers to leverage Objective-C libraries and build full-featured macOS applications. Version 12.1 is currently available, with the project demonstrating active and sustainable maintenance, receiving regular updates.
Warnings
- breaking PyObjC 12.0 dropped support for Python 3.9. Ensure your Python environment is 3.10 or later.
- breaking PyObjC 11.0 dropped support for Python 3.8. Python 3.9 and later were supported in this version.
- breaking The `IMServicePlugIn` framework bindings were removed in PyObjC 10.0, as the framework was deprecated in macOS 10.13 and removed in macOS 14.
- breaking Changes in PyObjC 10.3 initially prevented the use of `__init__` when a user implemented `__new__`. While partially re-enabled in 10.3.1 for custom `__new__` implementations, classes relying on the `__new__` provided by PyObjC still cannot use `__init__`.
- breaking PyObjC 11.1 aligned its behavior with `clang`'s Automatic Reference Counting (ARC) documentation for initializer methods. Methods in the 'init' family now correctly steal a reference to `self` and return a new reference.
- gotcha Key-Value Observing (KVO) with Python collections (lists, dictionaries) is not automatically supported. For instance variables that will be observed and contain a sequence or mapping, use `NSMutableArray` or `NSMutableDictionary` instances instead of Python lists or dictionaries.
- gotcha Developing PyObjC applications requires Xcode Command Line Tools to be installed on macOS for building and compilation.
Install
-
pip install pyobjc-framework-cocoa
Imports
- Foundation
import Foundation
- AppKit
import AppKit
- objc
import objc
- PyObjCTools
from PyObjCTools import AppHelper
Quickstart
import Foundation
import objc
def main():
print(f"Current date and time: {Foundation.NSDate.date()}")
Foundation.NSLog("Hello from PyObjC!")
if __name__ == "__main__":
main()