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:\mingw\bin 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 \PythonXY\Lib\distutils 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 build\scripts-2.5\cython.py -> C:\python251\Scripts running install_egg_info Writing C:\python251\Lib\site-packages\Cython-0.9.6.12-py2.5.egg-info
Checking the installation
7) Go to the Cython demos dir
8) Comment out the submodule demo in Setup.py (This demo doesn't come with the standard distribution)
9) 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 Python25\libs 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:
- Python version is 2.4 and is installed on C:/Python24/ (default)
- Mingw Is already setup
- dlltool works (dlltool is part of MinGW utilities)
Same steps applies to different python version, just change 24 to your XX version.
Procedure:
Download pexport
- Get python24.dll (somewhere in your hardrive, search for python24.dll).
Run : pexports python24.dll > python24.def
- This will extract all symbols from python24.dll and write them into python24.def.
- Run : dlltool --dllname python24.dll --def python24.def --output-lib libpython24.a
- This will create libpython24.a
- Copy libpython24.a to c:\python25\libs\ (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:
python25.def (XXX this is not so clean but works
)
- 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.
