{"id":5413,"library":"pyobjc","title":"PyObjC","description":"PyObjC is a bridge between Python and Objective-C, allowing Python scripts to use and extend existing Objective-C class libraries, most importantly Apple's Cocoa. It enables the development of full-featured macOS applications in pure Python and seamless integration with Objective-C, C, and C++ code. PyObjC (version 12.1) is actively maintained and typically releases new versions aligning with macOS SDK updates and Python version support.","status":"active","version":"12.1","language":"en","source_language":"en","source_url":"https://github.com/ronaldoussoren/pyobjc","tags":["macos","objective-c","cocoa","ui","bridge","interoperability","apple"],"install":[{"cmd":"pip install pyobjc","lang":"bash","label":"Install core PyObjC and common frameworks"},{"cmd":"pip install pyobjc-core pyobjc-framework-Cocoa pyobjc-framework-AVFoundation","lang":"bash","label":"Install specific components"}],"dependencies":[{"reason":"The fundamental bridge implementation for Python-Objective-C interoperability.","package":"pyobjc-core","optional":false},{"reason":"Specialized packages providing Python bindings for specific macOS frameworks (e.g., Cocoa, AppKit, Foundation, AVFoundation). The 'pyobjc' meta-package installs many of these.","package":"pyobjc-framework-*","optional":true}],"imports":[{"note":"Core bridge module for PyObjC utilities and functions.","symbol":"objc","correct":"import objc"},{"note":"Commonly imported framework for basic Objective-C types and utilities.","symbol":"Foundation","correct":"from Foundation import NSObject, NSString"},{"note":"Commonly imported framework for macOS user interface elements (Cocoa applications).","symbol":"AppKit","correct":"from AppKit import NSApplication, NSWindow"}],"quickstart":{"code":"import objc\nfrom Foundation import NSString, NSArray\n\n# Working with Objective-C classes\nhello = NSString.stringWithString_(\"Hello, World!\")\nprint(f\"Hello string length: {hello.length()}\") # Expected: 13\n\n# Creating and using Objective-C objects\n# Note the 'None' to signify the end of the variadic arguments for arrayWithObjects_\nmy_array = NSArray.arrayWithObjects_(\"foo\", \"bar\", \"baz\", None)\nprint(f\"Array count: {my_array.count()}\") # Expected: 3\nprint(f\"Object at index 1: {my_array.objectAtIndex_(1)}\") # Expected: bar\n\n# Using the bridge constants\nprint(f\"objc.YES: {objc.YES}\") # Expected: True\nprint(f\"objc.NO: {objc.NO}\")  # Expected: False\nprint(f\"objc.nil: {objc.nil}\") # Expected: None\n\n# Proper memory management with autorelease pools (good practice for intensive ops)\nwith objc.autorelease_pool():\n    temp_strings = []\n    for i in range(5):\n        temp_string = NSString.stringWithFormat_(\"Item %d\", i)\n        temp_strings.append(temp_string)\n    print(f\"Temporary strings created: {len(temp_strings)}\")\n    # Objects are automatically released when the pool exits","lang":"python","description":"This example demonstrates fundamental PyObjC interactions: creating Objective-C `NSString` and `NSArray` objects, calling their methods, accessing PyObjC's `objc.YES`/`NO`/`nil` constants, and using `objc.autorelease_pool` for memory management."},"warnings":[{"fix":"Upgrade your Python interpreter to 3.10 or later for PyObjC 12.x. Always check the official PyObjC documentation for the latest supported Python versions for your PyObjC release.","message":"PyObjC has dropped support for older Python versions in recent major releases. Version 12.0 dropped Python 3.9, and Version 11.0 dropped Python 3.8. Ensure your Python environment meets the minimum requirement (Python 3.10+ for PyObjC 12.x).","severity":"breaking","affected_versions":"10.0, 11.0, 12.0"},{"fix":"Review Objective-C initializer method calls and reference counting in your Python code, especially if manually managing object lifetimes or dealing with 'partially initialized' objects. Prefer the Pythonic `SomeClass(...)` initialization over `SomeClass.alloc().init()` where possible.","message":"PyObjC 11.1 introduced significant changes to align with `clang`'s documentation for Automatic Reference Counting (ARC) for initializer methods. Methods in the 'init' family now correctly model stealing a reference to `self` and returning a new one. This can cause reference counting bugs to surface or lead to crashes if existing code relied on previous, incorrect behavior.","severity":"breaking","affected_versions":">=11.1"},{"fix":"Always convert Objective-C selectors by replacing colons with underscores. The number of underscores in the Python method name must match the number of arguments (including the implicit `self`).","message":"Objective-C method names containing colons (e.g., `doSomething:withSomethingElse:`) are translated to Python by replacing colons with underscores (e.g., `doSomething_withSomethingElse_`). Each underscore signifies an argument. Missing an underscore or an argument will result in `AttributeError` or unexpected behavior.","severity":"gotcha","affected_versions":"All"},{"fix":"If implementing custom `__new__` methods in Objective-C subclasses, be cautious with `__init__` behavior. Prefer using Objective-C's `init` methods directly or the `SomeClass(...)` factory when possible, rather than relying on Python's `__init__` for complex initialization logic.","message":"Using `__init__` in Python subclasses of Objective-C classes can have subtle interactions with `__new__`. While PyObjC 10.3 initially removed support for calling `__init__` when a user-defined `__new__` was present, this was partially reintroduced in 10.3.1. Code relying on PyObjC's provided `__new__` still cannot use `__init__`.","severity":"gotcha","affected_versions":">=10.3"},{"fix":"If your application explicitly uses `AVFAudio` components, you may need to adjust your imports and ensure `pyobjc-framework-AVFAudio` is installed, although it's typically included with `pyobjc-framework-AVFoundation`.","message":"The `AVFAudio` framework, previously merged into `AVFoundation` bindings, was split into its own top-level package (`pyobjc-framework-AVFAudio`) starting with PyObjC 12.0.","severity":"breaking","affected_versions":">=12.0"},{"fix":"Ensure you are using a standard CPython installation on macOS for PyObjC development.","message":"PyObjC is specifically designed for CPython on macOS. It does not support alternative Python runtimes like PyPy, Jython, or IronPython, and this is unlikely to change.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-13T00:00:00.000Z","next_check":"2026-07-12T00:00:00.000Z"}