Python compiler that transforms Python code into C/C++ extensions for high-performance computing.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Cython provides three main command line tools for compiling, building, and debugging Cython code. These tools offer complete control over the compilation process and support various output formats and optimization levels.
The main Cython compiler that converts .pyx files to C/C++ source code.
cython [options] sourcefile.pyx
Options:
-o FILE, --output-file FILE Output file name
-D, --output-dir DIR Output directory
-3 Compile for Python 3
-2 Compile for Python 2
--cplus Generate C++ code
-X DIRECTIVE Set compiler directive
-E, --directive DIRECTIVE Set compiler directive (alternative form)
-s, --option OPTION Set compiler option
-p, --embed-pos-in-filename Embed source position in filename
-z, --pre-import MODULE Pre-import module
--cleanup LEVEL Cleanup intermediate files (0-3)
-w, --working DIR Working directory
-v, --verbose Verbose output
-l, --include-dir DIR Add include directory
-I DIR Add include directory (alternative)
--gdb Generate debugging information
--gdb-outdir DIR GDB debugging output directory
-a, --annotate Generate annotated HTML output
--annotate-coverage COVERAGE Use coverage data for annotation
-f, --force Force recompilation
--timestamps Only compile newer sources
-q, --quiet Quiet operation
--fast-fail Stop on first error
--warning-errors Treat warnings as errors
--Werror Treat warnings as errors (alternative)
-M Generate dependency file
--depfile FILE Dependency output fileHigh-level build tool that compiles .pyx files into extension modules ready for installation.
cythonize [options] sources...
Positional arguments:
sources Source files (.pyx, .py) or glob patterns
Options:
-b, --build Build extension modules
-i, --inplace Build extensions in-place
-j N, --parallel N Parallel compilation (N processes)
-f, --force Force recompilation
-q, --quiet Quiet operation
-k, --keep-going Continue despite errors
-X DIRECTIVE Set compiler directive
--lenient Lenient error handling
--no-docstrings Remove docstrings
-M Generate dependency files
--depfile Dependency file template
-s OPTION Set compiler option
--annotate Generate annotated HTML
-3 Compile for Python 3
-2 Compile for Python 2
--cplus Generate C++ code
--fast-fail Stop on first error
--verbose Verbose output
-h, --help Show helpGDB-based debugger for Cython code that allows debugging both Python and C-level code.
cygdb [options] [path-to-project-directory] [gdb-args...]
Positional arguments:
path-to-project-directory Path to project with cython_debug/ directory
gdb-args Additional arguments passed to GDB
Options:
--gdb-executable GDB GDB executable to use (default: gdb)
--verbose Verbose output
-h, --help Show help# Compile a single .pyx file to C
cython my_module.pyx
# Compile for Python 3 with C++ output
cython -3 --cplus my_module.pyx -o my_module.cpp
# Generate annotated HTML for performance analysis
cython -a my_module.pyx# Compile with specific directives
cython -X boundscheck=False -X wraparound=False my_module.pyx
# Include debugging information
cython --gdb my_module.pyx
# Compile with custom include directories
cython -I /usr/local/include -I ./headers my_module.pyx
# Generate dependency files
cython -M --depfile my_module.dep my_module.pyx# Build extension module in-place
cythonize -i my_module.pyx
# Build with parallel compilation
cythonize -j 4 -b src/*.pyx
# Build with specific Python version
cythonize -3 -b my_module.pyx
# Build C++ extensions
cythonize --cplus -b *.pyx# Force rebuild with annotations
cythonize -f --annotate -b my_module.pyx
# Build with compiler directives
cythonize -X language_level=3 -X boundscheck=False -b *.pyx
# Build with custom options and keep going on errors
cythonize -k -s -j 8 -b src/**/*.pyx
# Quiet build with dependency tracking
cythonize -q -M -b my_package/*.pyx# Debug in current directory (requires cython_debug/ folder)
cygdb
# Debug specific project
cygdb /path/to/my/project
# Debug with custom GDB executable
cygdb --gdb-executable gdb-multiarch
# Debug with additional GDB arguments
cygdb . --args python my_script.py arg1 arg2# 1. Write Cython code in my_module.pyx
# 2. Build with debugging and annotations
cythonize -i --gdb --annotate my_module.pyx
# 3. Test the module
python -c "import my_module; print(my_module.test_function())"
# 4. Debug if needed
cygdb
# 5. Optimize based on annotations
# Open my_module.html in browser to see performance bottlenecks# Compile with optimizations for production
cythonize -X boundscheck=False \
-X wraparound=False \
-X cdivision=True \
-X language_level=3 \
-j 8 -b src/*.pyx# Set environment variables for cross-compilation
export CC=arm-linux-gnueabihf-gcc
export CXX=arm-linux-gnueabihf-g++
# Compile with custom include paths
cythonize -I /usr/arm-linux-gnueabihf/include \
-b my_module.pyx# Makefile example
CYTHON = cython
CYTHONIZE = cythonize
%.c: %.pyx
$(CYTHON) -3 $< -o $@
build: src/*.pyx
$(CYTHONIZE) -3 -b $^
debug: src/*.pyx
$(CYTHONIZE) -3 --gdb --annotate -i $^
clean:
rm -f src/*.c src/*.cpp src/*.html src/*.so# CMakeLists.txt example
find_program(CYTHON_EXECUTABLE cython)
function(add_cython_module module_name pyx_file)
get_filename_component(pyx_file_we ${pyx_file} NAME_WE)
set(c_file "${CMAKE_CURRENT_BINARY_DIR}/${pyx_file_we}.c")
add_custom_command(
OUTPUT ${c_file}
COMMAND ${CYTHON_EXECUTABLE} -3 ${pyx_file} -o ${c_file}
DEPENDS ${pyx_file}
)
add_library(${module_name} MODULE ${c_file})
endfunction()Common environment variables that affect the command line tools:
# Compiler selection
export CC=clang
export CXX=clang++
# Python configuration
export PYTHON_CONFIG=python3-config
# Build flags
export CFLAGS="-O3 -march=native"
export CXXFLAGS="-O3 -march=native"
# Include paths
export CPATH="/usr/local/include:/opt/include"
# Library paths
export LIBRARY_PATH="/usr/local/lib:/opt/lib"Install with Tessl CLI
npx tessl i tessl/pypi-cython