Cython: C-Extensions for Python

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

Cython is an optimizing static compiler for both the Python programming language and the extended Cython programming language, making writing C extensions for Python as easy as Python itself. Current version: 3.2.4. Release cadence: Regular updates with active development and maintenance.

pip install cython
error ModuleNotFoundError: No module named 'Cython'
cause This error occurs when the Cython package itself is not installed in the Python environment you are using, or it's installed in a different environment or Python version.
fix
Ensure Cython is installed in your active Python environment by running: pip install Cython
error error: Microsoft Visual C++ 14.0 or greater is required.
cause On Windows, compiling Cython extensions (which generate C/C++ code) requires a compatible C++ compiler, typically provided by the Microsoft C++ Build Tools or a Visual Studio installation.
fix
Install the 'Desktop development with C++' workload from Visual Studio Installer, or specifically the 'Microsoft C++ Build Tools' (available at visualstudio.microsoft.com/visual-cpp-build-tools/). Ensure your environment variables are correctly set for the compiler.
error AttributeError: type object 'your_module.YourCythonClass' has no attribute '__reduce_cython__'
cause This error typically arises when trying to pickle or copy a Cython extension type that does not have proper serialization methods (`__reduce_cython__` and `__setstate_cython__`) defined, often after upgrading Cython or Python, or when using libraries like PyInstaller.
fix
For pickling custom extension types, you need to explicitly implement __reduce_cython__ and __setstate_cython__ methods in your Cython class, or use the @cython.auto_pickle(True) decorator if applicable (though explicit implementation is generally safer for complex types).
error Segmentation fault
cause A segmentation fault in Cython code usually indicates a low-level memory access error, such as reading or writing beyond allocated memory bounds, using uninitialized C pointers, or incorrect memory management when interacting with C libraries.
fix
Debug the Cython code to identify memory-related issues. Common steps include enabling Cython directives like boundscheck=True and wraparound=True during development, carefully managing C pointers and malloc/free, and using tools like gdb for C-level debugging.
breaking Support for Python 2.7 - 3.7 was removed in Cython 3.3.0a0, along with large chunks of legacy code.
fix Upgrade to Python 3.8 or later.
gotcha Using the pure Python syntax for static type declarations requires a recent Cython 3 release due to significant improvements over the 0.29.x releases.
fix Upgrade to the latest Cython 3 release.
breaking The script uses Cython-specific C-style parameter declarations (e.g., `int x`), which are invalid in standard Python and will result in a `SyntaxError`. This syntax is only recognized by the Cython compiler.
fix Compile the script with Cython or rewrite the code using standard Python type hints (e.g., `x: int`).
python os / libc status wheel install import disk
3.10 alpine (musl) - - 0.01s 30.7M
3.10 slim (glibc) - - 0.01s 32M
3.11 alpine (musl) - - 0.04s 34.6M
3.11 slim (glibc) - - 0.03s 36M
3.12 alpine (musl) - - 0.03s 26.2M
3.12 slim (glibc) - - 0.03s 27M
3.13 alpine (musl) - - 0.03s 25.9M
3.13 slim (glibc) - - 0.02s 27M
3.9 alpine (musl) - - 0.01s 30.2M
3.9 slim (glibc) - - 0.01s 32M

A simple example demonstrating the use of Cython's cfunc decorator to define a C function in Python.

import cython

@cython.cfunc
@cython.exceptval(-1)
def add(int x, int y):
    return x + y

print(add(2, 3))