Getting started with cython

Hello World

A Cython pyx file needs to be translated to C code (cythonized) and compiled before it can be used from Python. A common approach is to create an extension module which is then imported in a Python program.


For this example we create three files:

  • hello.pyx contains the Cython code.
  • is a Python script that uses the hello extension.
  • is used to compile the Cython code.


from libc.math cimport pow

cdef double square_and_add (double x):
    """Compute x^2 + x as double.

    This is a cdef function that can be called from within
    a Cython program, but not from Python.
    return pow(x, 2.0) + x

cpdef print_result (double x):
    """This is a cpdef function that can be called from Python."""
    print("({} ^ 2) + {} = {}".format(x, x, square_and_add(x)))

# Import the extension module hello.
import hello

# Call the print_result method 

from distutils.core import Extension, setup
from Cython.Build import cythonize

# define an extension that will be cythonized and compiled
ext = Extension(name="hello", sources=["hello.pyx"])


This can be done by, using cython hello.pyx to translate the code to C and then compile it using gcc. An easier way is to let distutils handle this:

$ ls
$ python build_ext --inplace
$ ls
build  hello.c  hello.pyx

The shared object (.so) file can be imported and used from Python, so now we can run the

$ python
(23.0 ^ 2) + 23.0 = 552.0 

Installing Cython

To use Cython two things are needed.The Cython package itself, which contains the cython source-to-source compiler and Cython interfaces to several C and Python libraries (for example numpy). To compile the C code generated by the cython compiler, a C compiler is needed.

Step 1: Installing Cython

System Agnostic

Cython can be installed with several system agnostic package management systems. These include:

  1. PyPI via pip or easy_install:

    $ pip install cython
    $ easy_install cython
  2. anaconda using conda:

    $ conda install cython
  3. Enthought canopy using the enpkg package manager:

    $ enpkg cython

Also the source code can be downloaded from github and installed manually using:

$ python install

Ubuntu, Debian

For Ubuntu the packages cython and cython3 are available. Note that these provide an older version than the installation options mentioned above.

$ apt-get install cython cython3


For Windows, a .whl file that can be installed using pip is provided by a third party. Details on installing a .whl file on Windows can be found here.

Step 2: Installing a C Compiler

To compile the C files generated by Cython, a compiler for C and C++ is needed. The gcc compiler is recommended and can be installed as follows.

Ubuntu, Debian

The build-essential package contains everything that is needed. It can be installed from the repositories using:

$ sudo apt-get install build-essential


The XCode developer tools contain a gcc like compiler.


MinGW (Minimalist GNU for Windows) contains a Windows version of gcc. The compiler from Visual Studio can also be used.