Cython初探

用distutils构造Cpthon模块

第一步:先写一个hello.pyx脚本

def say_hello_to(name):
    print("Hello %s!" % name)


第二步:写一个和编译有关的 setup.py脚本

from distutils.core import setup
from Cython.Build import cythonize

setup(
  name = 'Hello world app',
  ext_modules = cythonize("hello.pyx"),
)

第三步:编译

python setup.py build_ext --inplace


第四步:引用

from hello import say_hello_to
print(say_hello_to('Cpython Demo'))

Note:对于python2而言,脚本所在文件夹如果不是英文则需要修改编码方式

Link:http://cython.readthedocs.io/en/latest/src/quickstart/build.html

posted @ 2022-11-13 22:37  dlhl  阅读(24)  评论(0)    收藏  举报