PyObjC is a bridge between Python and Objective-C that allows full featured Cocoa applications to be written in pure Python.
npx @tessl/cli install tessl/pypi-pyobjc@10.3.0PyObjC is a comprehensive Python-to-Objective-C bridge that enables seamless integration between Python and Objective-C code on macOS. It allows developers to create full-featured Cocoa applications in pure Python, access macOS system frameworks, and mix Python with Objective-C, C, and C++ code.
pip install pyobjcimport objcFor utility tools:
from PyObjCTools import KeyValueCoding, TestSupportimport objc
from Foundation import NSString, NSArray
# Working with Objective-C classes
hello = NSString.stringWithString_("Hello, World!")
print(hello.length()) # 13
# Creating and using Objective-C objects
array = NSArray.arrayWithObjects_("foo", "bar", "baz", None)
print(array.count()) # 3
print(array.objectAtIndex_(1)) # bar
# Using the bridge constants
print(objc.YES) # True
print(objc.NO) # False
print(objc.nil) # None
# Proper memory management with autorelease pools
with objc.autorelease_pool():
# Intensive Objective-C operations that create temporary objects
for i in range(1000):
temp_string = NSString.stringWithFormat_("Item %d", i)
# Objects are automatically released when pool exits
# Manual pool management (less common)
objc.recycleAutoreleasePool()PyObjC consists of two main components:
PyObjC provides comprehensive coverage of macOS frameworks through specialized packages:
Core Frameworks:
pyobjc-framework-Cocoa - Foundation, AppKit, CoreFoundation (essential UI and system APIs)pyobjc-framework-CoreData - Object-relational mapping and data persistencepyobjc-framework-CoreGraphics - 2D graphics rendering and PDF generationMedia and Graphics:
pyobjc-framework-AVFoundation - Audio/video capture, playback, and editingpyobjc-framework-Metal - GPU programming and graphics accelerationpyobjc-framework-Vision - Computer vision and image analysispyobjc-framework-CoreImage - Image processing and filteringWeb and Networking:
pyobjc-framework-WebKit - Web browser engine integrationpyobjc-framework-Network - Modern networking APIspyobjc-framework-CFNetwork - Core networking functionalityDevelopment Tools:
pyobjc-framework-Quartz - PDF and graphics utilitiespyobjc-framework-ScriptingBridge - Application automationEach framework package uses lazy loading for optimal performance and automatically handles framework initialization, type bridging, and method signature resolution. The bridge provides seamless integration with over 100 system frameworks while maintaining Pythonic interfaces and automatic memory management.
Fundamental bridge functionality including class lookup, runtime access, memory management, and bridge configuration. These functions provide the foundation for all Python-Objective-C interoperability.
def lookUpClass(name: str): ...
def getClassList(): ...
def recycleAutoreleasePool(): ...
options: object # Bridge configuration (objc.options.verbose = True/False)Decorators and utilities for creating Objective-C methods, defining method signatures, creating categories, and handling protocol conformance. Essential for creating Python classes that integrate with Objective-C.
def signature(signature: str): ...
def callbackFor(signature: str): ...
def category(baseClass): ...
def synthesize(name: str, copy=None): ...Dynamic loading of macOS frameworks and libraries, bundle resource access, and framework initialization. Enables runtime access to system frameworks and custom libraries.
def dyld_framework(path: str): ...
def pathForFramework(name: str): ...
def inBundle(bundlePath: str): ...Type encoding, signature parsing, struct type creation, and bridge support utilities. Handles the complex type conversion between Python and Objective-C type systems.
def splitSignature(signature: str): ...
def createStructType(name: str, signature: str, typeids): ...
def registerMetaDataForSelector(classname: str, selector: str, metadata): ...Protocol management, formal and informal protocol definition, and protocol conformance checking. Essential for implementing Objective-C protocols in Python.
def protocolNamed(name: str): ...
def informal_protocol(name: str, selectors): ...
def formal_protocol(name: str, supers, selectors): ...Key-Value Coding, testing support, signal handling, and other utility functions. Provides additional functionality for common PyObjC development patterns.
def getKey(object, key: str): ...
def setKey(object, key: str, value): ...
class TestCase: ...Dynamic method addition to existing Objective-C classes through categories. Enables extending system frameworks and third-party classes without subclassing.
def category(baseClass): ...
def classAddMethod(targetClass, method, targetMethod): ...
class Category: ...Categories and Method Addition
# Bridge Constants
nil = None
YES = True
NO = False
# Core Classes
class NSObject:
"""Base class for all Objective-C objects"""
class selector:
"""Represents an Objective-C selector (method name)"""
class objc_method:
"""Represents an Objective-C method with type information"""
class autorelease_pool:
"""Context manager for autorelease pool management"""
def __enter__(self): ...
def __exit__(self, exc_type, value, tp): ...
# Exception Classes
class error(Exception):
"""Base PyObjC exception"""
class nosuchclass_error(Exception):
"""Raised when an Objective-C class cannot be found"""
class internal_error(Exception):
"""Raised for internal PyObjC errors"""
# Type Encoding Constants
_C_ID: str # '@' - Object pointer
_C_CLASS: str # '#' - Class
_C_SEL: str # ':' - Selector
_C_CHR: str # 'c' - char
_C_INT: str # 'i' - int
_C_FLT: str # 'f' - float
_C_DBL: str # 'd' - double
_C_BOOL: str # 'B' - BOOL
_C_VOID: str # 'v' - void
_C_PTR: str # '^' - pointer