Cython workflow

1)
cython_file.pyx
Create a *.pyx file containing Python or Cython code. The *.pyx file contains the code that will be compiled to C.

2)
cython_setup.py
The setup *.py file contains the code that runs the compile process.

The setup *.py file:

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

pyx_filename = "cython_file.pyx"
new_module_name = "cython_module"

ext_modules = [Extension(new_module_name,
                         [pyx_filename],
                         include_dirs=[numpy.get_include()]
                        )]
setup(
  cmdclass = {'build_ext': build_ext},
  ext_modules = ext_modules
)

Lines 1 and 11 are necessary if you are using NumPy in the pyx file, otherwise they can be commented out.

3)
C:\...>cython_setup.py build_ext --inplace
In the command prompt from the setup file directory execute the code above. This will create a cython_file.c, a build directory, and a cython_module.pyd file.

3b)
error: Unable to find vcvarsall.bat
If you get this error. If may be because you have Visual C++ 2010 installed instead of the 2008 that it looks for. In the command prompt enter: SET VS90COMNTOOLS=%VS100COMNTOOLS%
This will redirect the search to the 2010 version.

3c)
The build directory and *.c file are not necessary and can be deleted. The *.pyd file is what you keep and import into projects.

4)
import cython_module
In a new project the C code module can be imported like any other module. The methods can be individually imported or accessed by cython_module.method()

PyCUDA fail and Cython win

I tried yet again to get PyCUDA working and failed. Getting an error that says the _driver module in pycuda is not there. The tips on installing that I’ve seen failed to solve the problem, so I’ll put this off for another time.

I can get CUDA working in C++ but I’m not too familiar with C++ and seem to always hit a wall when trying to use it. Always some package or library not found.

I did get Cython and scipy.weave working. If I need a speedup I guess this is the way to go for now.

When compiling the cython *.pyx file I got a error: Unable to find vcvarsall.bat
Typing SET VS90COMNTOOLS=%VS100COMNTOOLS% fixed this problem. A tip I found on stackoverflow that guides cython to my Visual C++ 2010 install instead of looking for 2008. After doing this, I got a different error, error: command ‘mt.exe’ failed with exit status 31 but it completed building the *.pyd file and I was then able to import and use it.

It works just like the tutorial at http://docs.cython.org/src/quickstart/build.html says except for having to enter the SET command before building.