python/cpython · error · ImportError

ObjC runtime library couldn't be loaded

Error message

ObjC runtime library couldn't be loaded

What it means

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.

Source

Thrown at Lib/_ios_support.py:16

import sys
try:
    from ctypes import cdll, c_void_p, c_char_p, util
except ImportError:
    # ctypes is an optional module. If it's not present, we're limited in what
    # we can tell about the system, but we don't want to prevent the module
    # from working.
    print("ctypes isn't available; iOS system calls will not be available", file=sys.stderr)
    objc = None
else:
    # ctypes is available. Load the ObjC library, and wrap the objc_getClass,
    # sel_registerName methods
    lib = util.find_library("objc")
    if lib is None:
        # Failed to load the objc library
        raise ImportError("ObjC runtime library couldn't be loaded")

    objc = cdll.LoadLibrary(lib)
    objc.objc_getClass.restype = c_void_p
    objc.objc_getClass.argtypes = [c_char_p]
    objc.sel_registerName.restype = c_void_p
    objc.sel_registerName.argtypes = [c_char_p]


def get_platform_ios():
    # Determine if this is a simulator using the multiarch value
    is_simulator = sys.implementation._multiarch.endswith("simulator")

    # We can't use ctypes; abort
    if not objc:
        return None

    # Most of the methods return ObjC objects
    objc.objc_msgSend.restype = c_void_p

View on GitHub (pinned to bc6749cc3b)

Solutions

  1. Import conditionally: only use _ios_support when sys.platform == 'ios' (guard with try/except ImportError otherwise).
  2. On Apple platforms, verify libobjc is findable: ctypes.util.find_library('objc') should return a path; fix SDK/DYLD paths if it returns None.
  3. In build/config scripts, key the import off sysconfig.get_platform() containing 'ios' rather than importing unconditionally.
  4. Treat absence of the module as 'not an iOS device' and fall back to generic platform detection.

Example fix

# before
import _ios_support  # ImportError on Linux CI: ObjC runtime library couldn't be loaded
info = _ios_support.get_platform_ios()

# after
import sys
if sys.platform == 'ios':
    import _ios_support
    info = _ios_support.get_platform_ios()
else:
    info = None
Defensive patterns

Strategy: try-catch

Validate before calling

import sys, ctypes.util

def ios_support_available() -> bool:
    return sys.platform == 'ios' and ctypes.util.find_library('objc') is not None

Try / catch

try:
    import _ios_support
except ImportError as e:
    if 'ObjC runtime' in str(e):
        _ios_support = None  # not an ObjC platform; use generic detection
    else:
        raise

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of python/cpython@bc6749cc3b (2026-08-14). Data as JSON: /api/errors/68be6fd0d4199f26. Report an issue: GitHub.