Wednesday, April 11, 2012

Installing a working Cython on Windows with MinGW: a comic tragedy

The following is the contents of a text file I have written to myself and will keep on-hand for the next time I have to install Cython:

Setting up Cython:
1. Works with 32-bit Python for some reason.  (I discovered why but forgot.)
2. Install 32-bit Python
3. Install 32-bit MinGW
3. Install setuptools for Win32
4. Write a batch file with the necessary paths, like containing:

SET PATH=C:\MinGW\bin;C:\MinGW\MSYS\1.0\local\bin;C:\MinGW\MSYS\1.0\bin;C:\Python27_32;C:\Python27_32\Scripts
start cmd

5. IN C:\(pythonpath)\Lib\distutils, create distutils.cfg, containing the text:

compiler = mingw32

6. In in the same directory, remove all instances of "-mno-cygwin".  This refers to a command line switch that has been removed in more recent versions of Cygwin/MinGW.

7. Run this to set up a command prompt window with the appropriate paths, then run: easy_install cython

8. Write a for the source modules to compile.  The modules should have the extension .pyx (instead of .py).  It should look something like:

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

ext_modules = [Extension("hello", ["hello.pyx"])]
// ext_modules can contain as many Extension entries as there are
//   modules to compile.  Change the module name and filename to
//   match each module to compile, of course.

// In the following, change name to something appropriate.
  name = 'Hello world app',
  cmdclass = {'build_ext': build_ext},
  ext_modules = ext_modules

9. Run from the pathed command window:
python build_ext --inplace

The module should now be importable.  It should work, at least.

The tragic/comic part of it all is the week it took me to get all this working. I actually did it all for the 64-bit version of Cython and almost got it working, only to be stopped by (yet another) mysterious error, only this one didn't have a solution. There's still one or two steps I ran into doing that that I haven't run into with the 32-bit version yet -- I'll probably run into those once I actually bring Cython to In Profundis' code (currently I've only compiled Hello World with it).

1 comment:

  1. Well, I’ve now wasted many hours trying to install Cython into my python27 distribution on MS Vista. The MinGW linker throws “cannot find -lmsvcr90” error at the end of the install process. I did remove all instances of the command line argument “-mno-cygwin” from the file as per the instructions. A web search indicated that I needed to install the Microsoft Visual C++ 2008 Redistributable Package:

    So I did. Still no joy. Fortunately, I just program as a hobby. Still, it’s frustrating – I was looking forward to futzing about with Cython.

    PS: One good thing did come out of it, I now have easy_install in my python installation. It’s a lot handier than using setup. Thanks for your blog post.