{"id":5023,"library":"pyobjc-framework-coreml","title":"PyObjC CoreML Framework","description":"PyObjC-framework-CoreML provides Python wrappers for Apple's CoreML framework on macOS, enabling Python applications to interact with Core ML models. It is part of the larger PyObjC project, which creates a bridge between Python and Objective-C, and is actively maintained with releases often aligned with macOS updates. The current version is 12.1.","status":"active","version":"12.1","language":"en","source_language":"en","source_url":"https://github.com/ronaldoussoren/pyobjc","tags":["macos","apple","coreml","bindings","objective-c","cocoa","machine-learning","ml"],"install":[{"cmd":"pip install pyobjc-framework-coreml","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Provides the core Python to Objective-C bridge functionalities.","package":"pyobjc-core","optional":false},{"reason":"Provides common Cocoa (Foundation/AppKit) types frequently used by CoreML.","package":"pyobjc-framework-cocoa","optional":false}],"imports":[{"note":"All CoreML classes and functions are typically exposed under the `CoreML` top-level module.","symbol":"CoreML","correct":"import CoreML"},{"note":"Specific CoreML classes can be imported directly for brevity.","symbol":"MLModel","correct":"from CoreML import MLModel"}],"quickstart":{"code":"import CoreML\nimport Foundation # Often needed for NSURL, NSBundle etc.\nimport os\n\n# --- This example demonstrates attempting to load a CoreML model ---\n# In a real scenario, 'your_model.mlmodel' would be a path to an actual Core ML model file.\n# For demonstration, we'll try a dummy path or look for a model in the app bundle.\n# Replace with the actual path to your .mlmodel file or ensure it's in your app bundle.\n\n# Option 1: Direct path (replace with actual path)\nmodel_path = os.environ.get('COREML_MODEL_PATH', '/tmp/your_model.mlmodel')\n\n# Option 2: Attempt to find in the main bundle (common for macOS apps)\n# mainBundle = Foundation.NSBundle.mainBundle()\n# model_path_from_bundle = mainBundle.pathForResource_ofType_(\"YourModelName\", \"mlmodel\")\n# if model_path_from_bundle:\n#     model_path = model_path_from_bundle\n\nprint(f\"Attempting to load CoreML model from: {model_path}\")\n\ntry:\n    # CoreML methods often follow Objective-C conventions, e.g., 'methodName_error_'\n    # for methods that take an NSError** parameter in Objective-C.\n    # The PyObjC bridge translates this into a tuple (result, error_object).\n    model, error = CoreML.MLModel.modelWithContentsOfURL_error_( \n        Foundation.NSURL.fileURLWithPath_(model_path), None\n    )\n    \n    if error:\n        print(f\"Error loading model: {error.localizedDescription()}\")\n    elif model:\n        print(f\"Successfully loaded CoreML model: {model}\")\n        print(f\"Model description: {model.modelDescription()}\")\n        # You can now interact with the model, e.g., for predictions:\n        # input_features = CoreML.MLFeatureProvider.alloc().init()\n        # prediction, pred_error = model.predictionFromFeatures_error_(input_features, None)\n        # if not pred_error: print(f\"Prediction: {prediction}\")\n    else:\n        print(\"Failed to load model without explicit error or model object.\")\n\nexcept Exception as e:\n    print(f\"An unexpected Python exception occurred: {e}\")\n\nprint(f\"\\nAccessing MLModel class directly: {CoreML.MLModel}\")","lang":"python","description":"This quickstart demonstrates how to import the `CoreML` framework and attempt to load an `MLModel` using PyObjC. It highlights the typical PyObjC pattern for handling Objective-C methods that return errors by reference. A functional example would require an actual `.mlmodel` file."},"warnings":[{"fix":"Upgrade Python to version 3.10 or higher.","message":"PyObjC 12.0 dropped support for Python 3.9. Users must ensure their environment uses Python 3.10 or later. PyObjC 11.0 also dropped support for Python 3.8.","severity":"breaking","affected_versions":">=12.0"},{"fix":"Review and update code interacting with Objective-C `init` methods to ensure correct reference handling, especially for complex object graphs or custom subclasses.","message":"PyObjC 11.1 aligned its memory management behavior for `init` methods with `clang`'s Automatic Reference Counting (ARC). Methods in the `init` family now correctly steal a reference to `self` and return a new one. This change might affect code relying on previous PyObjC reference counting behavior for initializers.","severity":"breaking","affected_versions":">=11.1"},{"fix":"If subclassing Objective-C classes, avoid implementing both `__new__` and `__init__` in Python for versions 10.3 and later, or carefully test behavior when a user-defined `__new__` is present.","message":"In PyObjC 10.3, a change broke the ability to use Python's `__init__` when a user also implements `__new__` for an Objective-C subclass. While 10.3.1 partially re-introduced this, classes relying on `__new__` provided by PyObjC still cannot use `__init__`.","severity":"gotcha","affected_versions":"10.3 - 10.3.x"},{"fix":"Migrate away from the `IMServicePlugIn` framework. There is no direct replacement in newer macOS versions via PyObjC.","message":"The `IMServicePlugIn` bindings were completely removed in PyObjC 10.0. This framework was deprecated by Apple in macOS 10.13 and removed in macOS 14.","severity":"breaking","affected_versions":">=10.0"},{"fix":"Avoid relying on Python 3.13's experimental free-threading features with PyObjC unless explicitly confirmed as stable and supported for your specific PyObjC version and frameworks.","message":"While PyObjC 11.0 introduced *experimental* support for free-threading (`PEP 703`) with Python 3.13, PyObjC 10.3 explicitly stated it does *not* support experimental free-threading in Python 3.13. Users should exercise caution and thoroughly test when using free-threading with PyObjC, especially with specific framework bindings.","severity":"gotcha","affected_versions":"10.3 - 11.x"}],"env_vars":null,"last_verified":"2026-04-12T00:00:00.000Z","next_check":"2026-07-11T00:00:00.000Z"}