1、安装
直接win+R输入cmd进入命令行,运行pip install cython(推荐)
或者安装官方Cython提供的压缩包:http://www.cython.org/#download,解压后,在解压目录运行:python setup.py install
2. 运行一个demo
创建hellocython.pyx,内容如下:
cdef extern from"stdio.h":
extern int printf(const char *format, ...)
def SayHello():
printf("hello,world\n")
return 0
代码非常简单,就是调用了C函数printf打印hello,world
3. 编译demo
最方便的是利用python的Distutils,先在hellocython目录下创建Setup.py:
from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize
setup(
name = 'hellocython',
ext_modules=cythonize([
Extension("hellocython", ["hellocython.pyx"]),
]),
)