安装(linux环境)
conda install cython
创建例子:
1.创建test目录
2.创建helloworld.pyx文件如下:
cdef extern from"stdio.h":
extern int printf(const char *format, ...)
def SayHello():
printf("hello,world\n")
- 编译
利用python的Distutils:
在helloworld目录下创建Setup.py文件如下:
from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize
setup(
name = 'helloworld',
ext_modules=cythonize([
Extension("helloworld", ["helloworld.pyx"]),
]),
)
编译:
python Setup.py build
安装:
python Setup.py install
安装后在当前目录生成build ,进入该目录的lib.xxxx目录中找到helloworld.xxx.so 文件,把该文件复制到最开始建立的test目录中
- 进入python环境执行
import helloworld
helloworld.SayHello()
最后输出:
hello,world