PyObjC PhotosUI Framework
pyobjc-framework-photosui provides Python bindings for Apple's PhotosUI framework on macOS, allowing Python developers to interact with system-level Photos user interface components. It is part of the larger PyObjC project, which acts as a bridge between Python and Objective-C. The library is actively maintained, with version 12.1 being the latest stable release, and it generally releases new versions in conjunction with major macOS SDK updates.
Warnings
- breaking Python 3.9 support was dropped in PyObjC 12.0. Python 3.8 support was dropped in PyObjC 11.0.
- breaking PyObjC 11.1 aligned the core bridge's 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.
- breaking The interaction between Python's `__init__` and `__new__` methods when subclassing Objective-C classes changed in PyObjC 10.3, leading to breakage for some projects. While partially reverted in 10.3.1, code relying on `__new__` provided by PyObjC cannot use `__init__`.
- deprecated The 'IMServicePlugIn' framework bindings were removed in PyObjC 10.0 as the framework itself was deprecated in macOS 10.13 and removed in macOS 14.
- gotcha PyObjC requires the Xcode Command Line Tools to be installed on macOS for building and sometimes even for installation.
- gotcha Objective-C object instantiation is a two-step process (`alloc()` then `init...()`). While PyObjC allows calling the class directly for many cases, understanding this underlying mechanism is crucial for complex or custom object creation.
Install
-
pip install pyobjc-framework-photosui
Imports
- PhotosUI
import PhotosUI
Quickstart
import PhotosUI
import Foundation
import AppKit
# Most PhotosUI interactions require a running Cocoa application.
# This snippet demonstrates basic import and class access.
# For a functional UI example, a full PyObjC app setup with NSApplication is required.
def main():
# Initialize a basic NSApplication (required for most Cocoa frameworks)
app = AppKit.NSApplication.sharedApplication()
# Access a PhotosUI class to confirm the framework is loaded
try:
# PHLivePhotoView is a common PhotosUI class introduced in iOS/macOS 10.11
_ = PhotosUI.PHLivePhotoView
print("Successfully imported PhotosUI and accessed PHLivePhotoView.")
except AttributeError:
print("Could not access PHLivePhotoView. Ensure macOS version supports PhotosUI and PyObjC is correctly installed.")
# Note: A real PhotosUI application would involve delegates, windows, and an event loop.
# app.run() # This would start the GUI event loop, blocking further execution.
if __name__ == '__main__':
main()