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

  

 
原文地址:https://www.cnblogs.com/deeplearning2015/p/10026379.html