To main page |

Cython and Mingw32

Cython is a compiled Python-like language for making Python modules.

Unlike Python it's compiled. Like Python, it has very simple syntax, which is derived from Python with addition of "cdef" keyword with very many meanings.

Why use it??

1) You can speed up OpenGL code using PyOpenGL up to 100 times
2) The code is converted to C and compiled, so you're not distributing source code

Simple example under Windows

1. Install Python (assumed)
2. Install Cython (via easy_install: "easy_install cython").
3. Get Mingw32 (with recent GCC) from here. Note that the original Mingw32 GCC (3.4) isn't anymore supported and has bugs.
4. Install Mingw32 (link above)
5. Setup PATH variable to include your C:/Mingw32/bin/ (or whereever you installed Mingw32)
6. Create file test.pyx
cdef int a
for a in range(10):
    b = a + 1
7. Create file
from distutils.core import setup 
from distutils.extension import Extension
from Pyrex.Distutils import build_ext
    name = "TestModule",
        Extension("test", ["test.pyx"])
    cmdclass = {'build_ext': build_ext}
8. Create file build.bat
python build_ext
9. Run build.bat

After that you get test.pyx file, which is imported into Python simply as
import test
The output test.pyx is a dll file if you need to know that.

This code will run maybe hundreds times faster than the same code under Python.

Remember that without cdef you're not going to see any real speed improvement!

See also

OpenGL and Cython