PyObjC CoreMotion Framework
PyObjC is a bridge between Python and Objective-C, enabling Python developers to write macOS applications and scripts using Apple's high-level system APIs. The `pyobjc-framework-coremotion` package provides Python wrappers for the CoreMotion framework on macOS, allowing access to device motion and environment-related data. The library is currently at version 12.1 and typically releases new major versions aligned with macOS SDK updates, usually around October/November, with minor bugfix releases as needed.
Warnings
- breaking PyObjC 12.0 dropped support for Python 3.9, and PyObjC 11.0 dropped support for Python 3.8. Users on these Python versions must pin to older PyObjC releases (e.g., PyObjC 11.1 for Python 3.9, PyObjC 10.3 for Python 3.8).
- breaking PyObjC 11.1 changed how it handles Automatic Reference Counting (ARC) for Objective-C initializer methods. Methods in the 'init' family now correctly steal a reference to `self` and return a new one, diverging from previous 'partially initialized' object handling.
- gotcha The behavior around Python's `__init__` and Objective-C's `__new__` for PyObjC classes can be subtle. While PyObjC 10.3 initially broke `__init__` for classes relying on PyObjC's `__new__`, version 10.3.1 restored `__init__` functionality if the class or its superclasses implement their own `__new__` method. Classes relying on the PyObjC-provided `__new__` still cannot use `__init__`.
- gotcha PyObjC 11.0 introduced experimental support for free-threading (PEP 703) with Python 3.13. While supported, users should be aware that this is an experimental feature and may have stability implications.
- gotcha Installing PyObjC from source (e.g., if binary wheels are unavailable or a specific SDK is needed) requires the Xcode Command Line Tools to be installed. Using an older macOS SDK than the one PyObjC was built against can lead to build errors.
Install
-
pip install pyobjc-framework-coremotion
Imports
- CoreMotion
import CoreMotion
- CMMotionManager
from CoreMotion import CMMotionManager
Quickstart
import CoreMotion
from Foundation import NSObject # Foundation is part of pyobjc-framework-Cocoa, which is usually installed with pyobjc
# A simple check to ensure the CoreMotion framework is available and a class is accessible.
# Actual use of CMMotionManager to get data would require an active NSApplication/NSRunLoop
# and appropriate user permissions on macOS.
print(f"CoreMotion module: {CoreMotion.__name__}")
if hasattr(CoreMotion, 'CMMotionManager'):
print(f"CMMotionManager class found: {CoreMotion.CMMotionManager}")
# Example: Attempting to instantiate CMMotionManager (requires an app context on macOS)
# manager = CoreMotion.CMMotionManager.alloc().init()
# print(f"CMMotionManager instance created: {manager}")
else:
print("CMMotionManager class not found. Ensure correct macOS version and PyObjC installation.")