For a class, I have written a solver for a numerical problem. My code runs in an IPython notebook, where most of the code is pure Python + NumPy + matplotlib, but the performance-critical parts are written in Cython (and included with the IPython %%cython
magic). The professor liked my solution and asked me to make it available to some undergraduate students he will be teaching about a similar subject in another class as reference material. The problem is, that I am not really sure about what would be the best way to distribute my code.
My main issue is, that most of the students probably run Windows. Compiling Cython code on Windows was a nightmare the last time I did it, since you will have to juggle with a couple of different C compilers until you find one that works. It can definitely not be expected from every student that wants to run my notebook to get a working Cython environment.
I had the idea to precompile the Cython part of the code (it does not need to be altered by the students) on a Windows machine and distribute the pyd file. So my question is:
Yes, you can expect the binary .pyd
file to run on a different Windows machine, as long as the the python version matches the version against which the Cython module was compiled, including x86/x64 differences.
Building extension modules on Windows is no longer as difficult as it once was.
The easiest is to use Anaconda Python which includes a compiler in the installer. It is the closest thing to "Just Works" currently available. It also makes it very easy to build a binary version for every Python version back to 2.6 if necessary, using the conda
command-line tool for creating virtual environments. You pretty much just activate an env, run the build (python setup.py bdist_wheel
to make a .whl
file), activate the next env for a different Python version, run the build, and so on. The wheel files you end up with (*.whl
) can be copied to other machines and then you simply pip install blah.whl
directly.
Using stock Python is also manageable, but requires a bit more work. For Python 2.7, you need to install the Windows SDK 7.0, and for Python 3.4 you need Windows SDK 7.1. These SDKs provide the compilers to build for both x86 and x64. Unfortunately, for Python 3.5 you need a different thing, Visual Studio 2015. My blog has a short entry that goes into more detail about getting set up for 2.7 and 3.4. (I still need to update it for 3.5).
There are a few tools for making it easier to compile .pyx
files directly. Cython itself provides a command line tool, cythonize
which you can use to build files like this:
> cythonize -b blah.pyx
There are other tools, like my easycython, which doesn't offer much more than cythonize
, but has a few conveniences like optional numpy support and optimized compiler flags by default. Either way, you can copy the .pyd
extension module itself and place it somewhere on the Python path. But wheels are better.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With