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()