{"record":{"id":"68be6fd0d4199f26","repo":"python/cpython","slug":"objc-runtime-library-couldn-t-be-loaded","errorCode":null,"errorMessage":"ObjC runtime library couldn't be loaded","messagePattern":"ObjC runtime library couldn't be loaded","errorType":"exception","errorClass":"ImportError","httpStatus":null,"severity":"error","filePath":"Lib/_ios_support.py","lineNumber":16,"sourceCode":"import sys\ntry:\n    from ctypes import cdll, c_void_p, c_char_p, util\nexcept ImportError:\n    # ctypes is an optional module. If it's not present, we're limited in what\n    # we can tell about the system, but we don't want to prevent the module\n    # from working.\n    print(\"ctypes isn't available; iOS system calls will not be available\", file=sys.stderr)\n    objc = None\nelse:\n    # ctypes is available. Load the ObjC library, and wrap the objc_getClass,\n    # sel_registerName methods\n    lib = util.find_library(\"objc\")\n    if lib is None:\n        # Failed to load the objc library\n        raise ImportError(\"ObjC runtime library couldn't be loaded\")\n\n    objc = cdll.LoadLibrary(lib)\n    objc.objc_getClass.restype = c_void_p\n    objc.objc_getClass.argtypes = [c_char_p]\n    objc.sel_registerName.restype = c_void_p\n    objc.sel_registerName.argtypes = [c_char_p]\n\n\ndef get_platform_ios():\n    # Determine if this is a simulator using the multiarch value\n    is_simulator = sys.implementation._multiarch.endswith(\"simulator\")\n\n    # We can't use ctypes; abort\n    if not objc:\n        return None\n\n    # Most of the methods return ObjC objects\n    objc.objc_msgSend.restype = c_void_p","sourceCodeStart":1,"sourceCodeEnd":34,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Lib/_ios_support.py#L1-L34","documentation":"ImportError raised at import time of the iOS-only helper module _ios_support (Lib/_ios_support.py) when ctypes is present but ctypes.util.find_library('objc') cannot locate libobjc. The module needs the Objective-C runtime (objc_getClass / sel_registerName) to answer platform questions on iOS; without the library it refuses to import rather than half-work.","triggerScenarios":"Importing _ios_support on a machine without an ObjC runtime — typical Linux/Windows CI containers, or a macOS/iOS build where the objc shared library is stripped or outside the loader path. Also sysconfig/platform detection code that unconditionally imports the module on every platform.","commonSituations":"Cross-platform scripts or test suites that import _ios_support without a platform guard; building CPython for a non-Apple target where a stale config pulls the module in; extracting Python version info on iOS simulators with broken SDK paths.","solutions":["Import conditionally: only use _ios_support when sys.platform == 'ios' (guard with try/except ImportError otherwise).","On Apple platforms, verify libobjc is findable: ctypes.util.find_library('objc') should return a path; fix SDK/DYLD paths if it returns None.","In build/config scripts, key the import off sysconfig.get_platform() containing 'ios' rather than importing unconditionally.","Treat absence of the module as 'not an iOS device' and fall back to generic platform detection."],"exampleFix":"# before\nimport _ios_support  # ImportError on Linux CI: ObjC runtime library couldn't be loaded\ninfo = _ios_support.get_platform_ios()\n\n# after\nimport sys\nif sys.platform == 'ios':\n    import _ios_support\n    info = _ios_support.get_platform_ios()\nelse:\n    info = None","handlingStrategy":"try-catch","validationCode":"import sys, ctypes.util\n\ndef ios_support_available() -> bool:\n    return sys.platform == 'ios' and ctypes.util.find_library('objc') is not None","typeGuard":null,"tryCatchPattern":"try:\n    import _ios_support\nexcept ImportError as e:\n    if 'ObjC runtime' in str(e):\n        _ios_support = None  # not an ObjC platform; use generic detection\n    else:\n        raise","preventionTips":["Guard the import behind sys.platform == 'ios'.","Check ctypes.util.find_library('objc') before relying on ObjC calls.","Keep platform-specific modules behind a thin abstraction so fallbacks are explicit."],"tags":["ios","ctypes","platform","import-error"],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}