将python代码编译成.so文件
Posted deeplearning2015
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将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))
以上是关于将python代码编译成.so文件的主要内容,如果未能解决你的问题,请参考以下文章
如何在Eclipse中如何用cygwin把C文件编译成so文件