将python代码编译成.so文件

https://moonlet.gitbooks.io/cython-document-zh_cn/content/ch1-basic_tutorial.html

add_num.pyx文件

def add_nums(ls):
    total = 0.
    for l in ls:
        total += l
    return total

  

set_up.py

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

setup(
    ext_modules = cythonize("add_num.pyx")
)

运行

python setup.py build_ext --inplace
linux上会生成add_num.so文件。
可以删除
add_num.pyx文件。

之后可以直接调用
import add_num

ls = [4,5,6,29]

print(add_num.add_nums(ls))

  

 

posted on 2018-11-27 14:51  懒得想名字  阅读(5435)  评论(0编辑  收藏  举报

导航