Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP


Stephen L. edited this page · 3 revisions
Clone this wiki locally

Cython works in a two-step process: the first is to convert a .pyx (Cython) file into a .c file, and then the second step is to compile this .c file into a .pyd file (equivalent of a .so on Linux).

So to get a fully working Cython install, you first need to install Cython so that .pyx files can be converted to .c files, and then you need to install a C compiler to be able to compile the .c files into .pyd.

The first step, installing Cython, is described here. For the second step, please see the following wiki entry:


Using Anaconda

Similarly to python(x,y), Anaconda is a Python distribution: this means that it's a package including the Python interpreter, along with commonly used libraries for scientific processing such as Numpy, IPython/Jupyter and Cython. The thing that makes Anaconda special compared to other distributions is that it also offers a new package manager, called conda.

To use Cython via Anaconda, you should first install a 32-bit Anaconda. 64-bit Anaconda will also work, but you may encounter issues when you will try to compile your Cython extensions.

Then, when your Anaconda is installed, open an "Anaconda Command Prompt" (see in the Start Menu), and type:

conda install cython

This will install the Cython extension. By default, IPython/Jupyter should already be installed with Anaconda, so that you can use the `%%cython --annotate` magic to optimize your scripts.

Now that Cython is installed, you can jump to the following wiki entry to install a C compiler:


Note that Anaconda installs libpython by default, which will automatically link setuptools to mingw or cygwin gcc compiler if you have one of them installed, but they won't allow you to compile .c files correctly on Windows. You should follow the wiki to install the Visual C++ for Python, which will solve the issues.

And then you can try that everything works OK by following the helloworld tutorial:

Using python(x,y)

Python(x,y) is a user-friendly one-step way to install Python and essential packages in Windows. When you install, make sure to check the boxes for the Cython package (obviously) as well as the MinGW compiler. (If you didn't include them when you first installed, you can still download them afterwards at the Python(x,y) Standard Plugin download page.)

When you run Cython, it tries to use the compiler from Microsoft Visual Studio 2008 (usually) by default, even if it's not installed.<<FootNote(Python (more specifically, the distutils package) remembers how it was built when it was built from its source code, and takes that to be the default compiler settings. When you download Python as a Windows binary (.exe), it was built from the source code on someone else's computer, not yours! That explains why it can default to a compiler that you have not installed. As of this writing (2012), the files downloadable from are compiled in: MSVS 2003 for Python 2.5; MSVS 2008 for Python 2.6 and 2.7 and (maybe) 3.*. Warning: Different versions of Microsoft Visual Studio are not compatible. The Express version of MSVS is compatible with the full version for MSVS 2008 but not 2003. The latest Python(x,y) is (maybe) compiled in MSVS 2008.)>> You can get the error message "Unable to find vcvarsall.bat". One option is to find this default compiler and install it: Usually the Microsoft Visual Studio 2008 Express edition is what you want. Alternatively, you can manually change the Cython compiler to MinGW. You can do this by changing the compiler defaults, as described in the sections below. But if you are in a hurry to get started, you can just tell Cython to use MinGW when you call it. For example, if you're using the pyximport method, you would add the following lines to the start of your Python script:

import pyximport
pyximport.install(setup_args={"script_args":["--compiler=mingw32"]}, reload_support=True)

As soon as these commands are executed, you will be able to use Python's normal "import" and "reload" commands with your Cython .pyx packages.

If you are using NumPy, the installation default seems to put some necessary header files in a place where the compiler can't find it. (You can get an error like "numpy/arrayobject.h: No such file or directory".) To fix this within python, you can modify the pyximport commands above to:

import numpy
import pyximport

Precompiled binaries

Christoph Gohlke maintains an unofficial set of Cython windows binaries for various Python versions, in both 32 and 64 bits.

Building Cython in Windows

This is a short description of the installation process on Windows, using the MinGW compiler (also known as mingw32, see The information has been collected from the web, but it is useful to have a (hopefully) coherent description in a single place. A very useful link is the following

MinGW Compiler

  1. Start by downloading the MinGW compiler at
  2. If you untar and ungzip with Winzip, be sure to uncheck the "smart tar" option.
  3. Add the path of the compiler to your PATH environment variable (eg: c:mingwbin should appear in the PATH)
  4. Now open the prompt and check if the compiler is correctly installed (gcc --version should be properly working)

5) It is very useful to tell distutils to use mingw. To achieve this, create a file named distutils.cfg (if you already don't have it) in your PythonXYLibdistutils and add to it the following lines:

compiler = mingw32

Installing Cython

The installation process is almost over:

6) Download and install cython: python install This should end with something like

running install_scripts copying -> C:python251Scripts running install_egg_info Writing C:python251Libsite-packagesCython-

6a) On "error: unrecognized command line option '-mno-cygwin'" read here:

Checking the installation

  1. Go to the Cython demos dir
  2. Comment out the submodule demo in (This demo doesn't come with the standard distribution)
  3. From the demos dir type:

python build_ext --inplace

now start up python and import primes and you're done :-)

Running with mingw32

You may need to modify to get it to run with mingw32, see:

Also, you may need to disable the refnanny:


To compile the .pyx files you need a .a version of the python library.

Luckily, Python 2.5 (but not all the previous versions) comes with a libpython25.a file in the Python25libs dir (the same folder of python25.lib). (FabrizioM: also python25 x64 version is missing the libpython25.a file ... )

but if you receive an error like

(.text+0x91): undefined reference to `_imp___Py_NoneStruct'
(.text+0x538): undefined reference to `_imp__PyExc_RuntimeError'
(.text+0x5c9): undefined reference to `_imp__PyString_Type'
(.text+0x5d0): undefined reference to `_imp__PyString_Type'
(.text+0x9a1): undefined reference to `_imp__PyInt_Type'

then your box is missing the required libpythonXX.a file and you need to create it.

Here it is how to do it:

In this procedure we will assume:
1. Python version is 2.4 and is installed on C:/Python24/ (default) 1. Mingw Is already setup 1. dlltool works (dlltool is part of MinGW utilities)

Same steps applies to different python version, just change 24 to your XX version.


1. Download pexport 1. Get python24.dll (somewhere in your hardrive, search for python24.dll). 1. Run : pexports python24.dll > python24.def

This will extract all symbols from python24.dll and write them into python24.def.
  1. Run : dlltool --dllname python24.dll --def python24.def --output-lib libpython24.a This will create libpython24.a
  1. Copy libpython24.a to c:python25libs(in the same directory as python24.lib).
If for whatever reason pexport does not works on your box(eg on x64), you can skip the first step and continue the procedure by using the attached defs:
libpython25.a is ALREADY present in the Python 2.5 distribution, so you shouldn't need this procedure.

MinGW + NumPy + pyximport at runtime

Setting all the environment variables necessary to use Cython with NumPy and MinGW at runtime is a handy alternative to having to instruct your users to muck around with setting their PATH and editing distutils.cfg manually. In other words, you'll be less likely to hear your users complain about the Unable to find vcvarsall.bat error.

Here is one way to do it:

import os
import numpy
import pyximport

if == 'nt':
    if os.environ.has_key('CPATH'):
        os.environ['CPATH'] = os.environ['CPATH'] + numpy.get_include()
        os.environ['CPATH'] = numpy.get_include()

    # XXX: we're assuming that MinGW is installed in C:\MinGW (default)
    if os.environ.has_key('PATH'):
        os.environ['PATH'] = os.environ['PATH'] + ';C:\MinGW\bin'
        os.environ['PATH'] = 'C:\MinGW\bin'

    mingw_setup_args = { 'options': { 'build_ext': { 'compiler': 'mingw32' } } }

elif == 'posix':
    if os.environ.has_key('CFLAGS'):
        os.environ['CFLAGS'] = os.environ['CFLAGS'] + ' -I' + numpy.get_include()
        os.environ['CFLAGS'] = ' -I' + numpy.get_include()


I have this in a module that I call Then, in other modules, I can do the following:

import cython
import mycythonmodule

where mycythonmodule.pyx is a Cython module.


Something went wrong with that request. Please try again.