Mypyc Runtime Library

raw JSON →
0.8.1 verified Tue May 12 auth: no python install: stale quickstart: verified

librt is the Mypyc runtime library, providing efficient C implementations of various Python standard library classes and functions. Mypyc leverages this library to produce faster C extensions when compiling Python code. It is primarily an internal dependency for `mypyc` and `mypy` (since version 1.19.0 for fixed-format cache serialization). The current version is 0.8.1, with development occurring in the main `mypy` repository and releases synced to the `librt` PyPI package.

pip install librt
error undefined reference to 'shm_open'
cause This error occurs during compilation or linking of C/C++ code (or Mypyc-compiled extensions that use C) when functions like `shm_open` or `timer_create` from the POSIX Realtime Extensions library (`librt`) are used, but the linker is not explicitly told to link against `librt`, or cannot find it.
fix
Ensure that -lrt is added to your linker flags when compiling, typically at the end of the command. For example, gcc -o myprogram myprogram.c -lrt.
error Fatal error: L6450U: Cannot find library rt.
cause This linking error indicates that the compiler or linker (often ARM's `armclang` or similar cross-compilers) cannot locate the `librt` library file on the system, even if the `-lrt` flag is provided.
fix
On Linux, ensure the librt development package is installed (e.g., libc6-dev or glibc-devel). If cross-compiling, verify that the target environment's librt is correctly configured and accessible in the toolchain's library paths. On Ubuntu, a common fix for librt.so not being found is to create a symbolic link: sudo ln -s /lib/x86_64-linux-gnu/librt.so.1 /usr/lib/x86_64-linux-gnu/librt.so.
error KeyError: 'ibmxl'
cause This error occurs during `librt` installation (often as a dependency of `mypy` version 1.19.x and later) on systems using the IBM XL compiler, because `librt`'s `build_setup.py` script does not have explicit support or configuration for this specific compiler type.
fix
This issue typically requires a patch to the librt build system to recognize and correctly handle the ibmxl compiler. Developers may need to check the mypyc/librt GitHub repository for updates or contribute a fix for the specific compiler environment.
error ERROR: Failed building wheel for librt Failed to build librt ERROR: Could not build wheels for librt
cause This common installation error occurs when `pip` attempts to install `librt` (or `mypy` which depends on it) and cannot compile the C extension due to missing build tools, incompatible compiler environments, or the unavailability of pre-built wheels for the specific operating system and Python version (e.g., on MSYS2/UCRT64/MinGW64 environments).
fix
Ensure you have a C compiler installed (e.g., build-essential on Debian/Ubuntu, Xcode Command Line Tools on macOS, or Visual Studio C++ build tools on Windows). For less common platforms like MSYS2, check if pre-built wheels become available, or if there are specific environment setup instructions for compiling C extensions. You might also try installing mypy using a Python version for which pre-built librt wheels are generally available.
breaking librt does not officially support PyPy. If your project targets PyPy and has `librt` as a dependency (e.g., through `mypy` for type checking), you may need to skip it in your CI matrix for PyPy environments.
fix Avoid using librt-dependent components in PyPy environments or configure CI to exclude librt-related checks/builds for PyPy.
gotcha librt's Application Binary Interface (ABI) is not explicitly versioned, and changes to it can lead to compatibility issues with `mypyc`-compiled extensions. This can cause breakage if `librt` is updated independently of `mypyc` or the compiled application.
fix Ensure `librt` is kept in sync with the `mypyc` version used for compilation. Pinning exact versions of `librt` and `mypyc` is recommended, especially in production environments or when distributing `mypyc`-compiled code.
gotcha librt is primarily an internal runtime component. Direct imports or reliance on its internal API in user-facing Python code are discouraged, as these interfaces are subject to change without standard deprecation warnings.
fix Treat librt as an implementation detail of mypyc. Do not directly import or use symbols from `librt` in your application code; instead, rely on the behavior of `mypyc`-compiled modules.
deprecated Mypy 1.19.0 introduced `librt` as a dependency for its fixed-format cache serialization. Older versions of `mypy` (before 1.19.0) did not require `librt` for this purpose, relying instead on JSON-based caching.
fix If upgrading `mypy` to 1.19.0 or newer, ensure `librt` is installed alongside it. For older `mypy` versions, `librt` is not required for cache functionality.
python os / libc status wheel install import disk
3.10 alpine (musl) - - - -
3.10 slim (glibc) - - - -
3.11 alpine (musl) - - - -
3.11 slim (glibc) - - - -
3.12 alpine (musl) - - - -
3.12 slim (glibc) - - - -
3.13 alpine (musl) - - - -
3.13 slim (glibc) - - - -
3.9 alpine (musl) - - - -
3.9 slim (glibc) - - - -

librt is a runtime library for code compiled with `mypyc`. Therefore, its 'quickstart' involves demonstrating how to use `mypyc` to compile Python code. The compiled code will then depend on `librt` at runtime. The example shows a simple Python function that `mypyc` could compile, implicitly leveraging `librt`'s optimizations.

# librt is implicitly used by mypyc-compiled modules.
# A typical quickstart involves using mypyc, which then utilizes librt.
# First, ensure librt (and mypy, which includes mypyc) are installed.
# pip install mypy mypyc

# example_module.py
# This module will be compiled by mypyc, which relies on librt at runtime.

def factorial(n: int) -> int:
    result = 1
    for i in range(1, n + 1):
        result *= i
    return result

if __name__ == '__main__':
    # In a real scenario, this would be compiled and imported as a C extension.
    # For illustration, let's just run it as normal Python.
    # To compile: mypyc example_module.py
    # This creates a C extension (e.g., example_module.cpython-XYZ.so) that depends on librt.
    print(f'Factorial of 5: {factorial(5)}')