CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-cython

Python compiler that transforms Python code into C/C++ extensions for high-performance computing.

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

build-system.mddocs/

Build System

The Cython build system provides tools for converting .pyx files into compiled extension modules. The core function cythonize handles the compilation process, while additional utilities support setuptools integration, parallel compilation, and advanced build configuration.

Capabilities

Core Build Function

The main entry point for building Cython extensions from source files.

def cythonize(module_list, exclude=None, nthreads=0, aliases=None, 
              quiet=False, force=None, language=None, 
              exclude_failures=False, show_all_warnings=False, **options):
    """Convert .pyx files to Extension objects for compilation.
    
    Args:
        module_list: List of .pyx files, glob patterns, or Extension objects
        exclude: List of file patterns to exclude from compilation
        nthreads: Number of parallel compilation threads (0 = CPU count)
        aliases: Dict mapping import names to file paths
        quiet: Suppress compilation output messages
        force: Force recompilation even if files haven't changed (None = auto-detect)
        language: Target language ('c' or 'c++')
        exclude_failures: Continue compilation despite individual file failures
        show_all_warnings: Show all compiler warnings including normally suppressed ones
        **options: Additional compiler options and directives
    
    Returns:
        List of Extension objects ready for distutils/setuptools
    """

Distutils Integration

Classes for integrating Cython compilation with Python's distutils and setuptools.

class build_ext:
    """Enhanced build_ext command with Cython support.
    
    Extends distutils.command.build_ext.build_ext to automatically
    compile .pyx files and handle Cython-specific build requirements.
    """

class Extension:
    """Extension class with Cython-specific features.
    
    Extends distutils.extension.Extension with support for Cython
    directives, include directories, and compilation options.
    """

Usage Examples

Basic setuptools Integration

from setuptools import setup
from Cython.Build import cythonize

setup(
    name="my_package",
    ext_modules=cythonize("src/*.pyx")
)

Advanced Build Configuration

from setuptools import setup, Extension
from Cython.Build import cythonize
import numpy

# Define extensions with specific options
extensions = [
    Extension(
        "my_module.fast_math",
        ["my_module/fast_math.pyx"],
        include_dirs=[numpy.get_include()],
        define_macros=[("NPY_NO_DEPRECATED_API", "NPY_1_7_API_VERSION")],
        extra_compile_args=["-O3"],
    )
]

setup(
    name="my_package", 
    ext_modules=cythonize(
        extensions,
        compiler_directives={
            'language_level': 3,
            'boundscheck': False,
            'wraparound': False,
            'cdivision': True,
        }
    )
)

Parallel Compilation

from setuptools import setup
from Cython.Build import cythonize
import multiprocessing

setup(
    name="my_package",
    ext_modules=cythonize(
        "src/*.pyx",
        nthreads=multiprocessing.cpu_count(),  # Use all CPU cores
        quiet=False,  # Show compilation progress
        force=True,   # Force recompilation
    )
)

Conditional Compilation

from setuptools import setup
from Cython.Build import cythonize
import os

# Use different compiler directives for debug vs release
debug_mode = os.environ.get('DEBUG', '0') == '1'

compiler_directives = {
    'language_level': 3,
    'boundscheck': debug_mode,
    'wraparound': debug_mode, 
    'cdivision': not debug_mode,
    'profile': debug_mode,
    'linetrace': debug_mode,
}

setup(
    name="my_package",
    ext_modules=cythonize(
        "src/*.pyx",
        compiler_directives=compiler_directives,
        gdb_debug=debug_mode,
    )
)

Language Selection

from setuptools import setup
from Cython.Build import cythonize

# Compile to C++
setup(
    name="my_cpp_package",
    ext_modules=cythonize(
        "src/*.pyx",
        language="c++",
        compiler_directives={'language_level': 3}
    )
)

Build with NumPy Integration

from setuptools import setup
from Cython.Build import cythonize
import numpy

setup(
    name="numeric_package",
    ext_modules=cythonize([
        Extension(
            "numeric_package.algorithms",
            ["numeric_package/algorithms.pyx"],
            include_dirs=[numpy.get_include()],
        )
    ]),
    install_requires=["numpy"]
)

Excluding Files and Error Handling

from setuptools import setup
from Cython.Build import cythonize

setup(
    name="my_package",
    ext_modules=cythonize(
        "src/**/*.pyx",
        exclude=["src/tests/*.pyx", "src/experimental/*.pyx"],
        exclude_failures=True,  # Continue despite individual file failures
        quiet=True,
    )
)

Using Cython Distutils Directly

from distutils.core import setup
from Cython.Distutils import build_ext
from Cython.Distutils.extension import Extension

setup(
    name="legacy_package",
    cmdclass={'build_ext': build_ext},
    ext_modules=[
        Extension("my_module", ["my_module.pyx"])
    ]
)

Install with Tessl CLI

npx tessl i tessl/pypi-cython

docs

build-system.md

c-cpp-interoperability.md

command-line-tools.md

core-language.md

debugging-profiling.md

import-system.md

index.md

ipython-integration.md

tile.json