Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

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 Python.org 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
pyximport.install(setup_args={"script_args":["--compiler=mingw32"],
                              "include_dirs":numpy.get_include()},
                  reload_support=True)

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 http://www.mingw.org/mingwfaq.shtml#faq-what) 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 http://sebsauvage.net/python/mingw.html

MinGW Compiler

  1. Start by downloading the MinGW compiler at http://www.mingw.org/download.shtml
  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:

[build]
compiler = mingw32

Installing Cython

The installation process is almost over:

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

running install_scripts copying buildscripts-2.5cython.py -> C:python251Scripts running install_egg_info Writing C:python251Libsite-packagesCython-0.9.6.12-py2.5.egg-info

6a) On "error: unrecognized command line option '-mno-cygwin'" read here: http://bugs.python.org/issue12641

Checking the installation

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

python setup.py build_ext --inplace

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

Running runtests.py with mingw32

You may need to modify runtests.py to get it to run with mingw32, see: http://thread.gmane.org/gmane.comp.python.cython.devel/8280/

Also, you may need to disable the refnanny: http://thread.gmane.org/gmane.comp.python.cython.devel/8325/focus=8332

Troubleshooting

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.

Procedure:

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).
Note::
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:
Note::
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 os.name == 'nt':
    if os.environ.has_key('CPATH'):
        os.environ['CPATH'] = os.environ['CPATH'] + numpy.get_include()
    else:
        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'
    else:
        os.environ['PATH'] = 'C:\MinGW\bin'

    mingw_setup_args = { 'options': { 'build_ext': { 'compiler': 'mingw32' } } }
    pyximport.install(setup_args=mingw_setup_args)

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

    pyximport.install()

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

import cython
import mycythonmodule

where mycythonmodule.pyx is a Cython module.

Compiling 64bit extension modules on Windows

64BitCythonExtensionsOnWindows

Footnotes

Something went wrong with that request. Please try again.