Sunday, July 24, 2011

First steps with Cython and Python(x,y)

Started today experimenting with Cython. With Cython you can write normal python code and compile it to C code. When you start to insert type information the speed up you get compared to normal python can be quite dramatic. The compiled c-code can be called from python (seemingly as if it was normal python code your calling). It is all nicely introduced here: Cython tutorial

On windows I am using Python(x,y). When you install it you have to tell the installer to include Cython. As c-compiler the Python (x,y) package includes mingw. Unfortunately Cython by default seems to expect a Visual C compiler. So the only way (so far) I found to use Cython is to run python from a normal command prompt when using the method (distutils) as described in the Cython Tutorial as follows:

python build_ext --inplace --compiler=mingw32

otherwise you get this error message:

error: Unable to find vcvarsall.bat


  1. Another option that totally avoids the command prompt: Within python, you can run

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

    and after that you can import Cython packages using normal "import" and "reload". (Even better is to add those commands to your so it auto-runs whenever you open python.)

    1. Forgot to say: See for thorough discussion.

    2. Thanks for the tip, I will give it a try the next time I do something with Cython