原文https://zhuanlan.zhihu.com/p/265462717
在这里插入图片描述

1 首先为什么要将python打包成so文件

Python是一种面向对象的解释型计算机程序设计语言,具有丰富和强大的库,使用其开发产品快速高效。python的解释特性是将py编译为独有的二进制编码pyc文件,然后对pyc中的指令进行解释执行,但是pyc的反编译却非常简单,可直接反编译为源码,当需要将产品发布到外部环境的时候,源码的保护尤为重要.因此需要将python文件打成so文件的目的就是为了保护源码。

2 下面就来实战如何将py文件打成so文件

(1) 准备工作:

首先需要安装如下相关包
python 安装:cython

pip install cython

linux 安装:python-devel,gcc
ubuntu版linux:

apt-get install python-devel
apt-get install gcc

(2) 新建一个待编译文件hello.py以及setup.py,

hello.py内容如下

def greet(str):
    return "hello " + str

setup.py内容如下

from distutils.core import setup
from Cython.Build import cythonize
setup(ext_modules = cythonize(["hello.py"]))

然后执行如下打包命令

python setup.py build_ext

执行完该命令后的会有什么变化呢?

首先会在同级目录下生成hello.c的文件以及一个build文件夹目录,在build文件夹目录里面存放着编译好的.so文件。
接下来来测试一下这个生成.so文件,首先写一个测试文件demo.py(和hello.py同一级目录)

from hello import greet
print(greet("tom"))

然后用命令python demo.py执行一下,结果是hello tom,一点都不意外,有同学就问,你这就测试完了吗。当然没有啊

其实真正测试之前,我们应该先将hello.py这个文件删除或者改一下名字,不然我们怎么知道

from hello import greet这条命令中的hello模块是hello.py文件还是对应生成的.so文件呢

接下来我将hello.py 删了,将打包编译生成的.so文件从build文件夹中移出来,移到与demo.py同一级目录。

接下来再去测试,当然结果仍然返回的是hello tom。这就说明hello模块是来源于我们的.so文件中。

其实还有一个问题,就是上面测试的是打包一个.so文件,如果有许多个.py文件需要我们打包编译,是不是要重复的编写多个setup.py文件,答案是肯定不需要!!!接下来我就说一下如何同时打包多个.py文件

(3)如何同时打包多个py文件
上面打包1个.py(hello.py)文件的命令如下:

from distutils.core import setup
from Cython.Build import cythonize
setup(ext_modules = cythonize(["hello.py"]))

现在如果我们有hello1.py,hello2.py,hello3.py,同时想将这么多个.py文件打包成.so文件,这时候我们就可以这样做,命令如下

from distutils.core import setup
from Cython.Build import cythonize
setup(ext_modules = cythonize(["hello1.py", "hello2.py","hello3.py"]))

其实不管你有多少个.py文件,统一写到cythonize里面就行了。

3. 批量转换成.so

#-* -coding: UTF-8 -* -
"""
执行前提:
    系统安装python-devel 和 gcc
    Python安装cython
编译某个文件夹:
    python py2so.py 项目文件夹
生成结果:
    目录 build 下
生成完成后:
    启动文件还需要py/pyc担当,须将启动的py/pyc拷贝到编译目录并删除so文件
"""
 
from distutils.command.build import build
import sys, os, shutil, time
from distutils.core import setup
from urllib import request
from Cython.Build import cythonize
import glob
 
starttime = time.time()
setupfile= os.path.join(os.path.abspath('.'), __file__)
 
def getpy(basepath=os.path.abspath('.'), parentpath='', name='', build_dir="build", 
          excepts=(), copyOther=False, delC=False):
    """
    获取py文件的路径
    :param basepath: 根路径
    :param parentpath: 父路径
    :param name: 文件/夹
    :param excepts: 排除文件
    :param copy: 是否copy其他文件
    :return: py文件的迭代器
    """
    fullpath = os.path.join(basepath, parentpath, name)
    for fname in os.listdir(fullpath):
        ffile = os.path.join(fullpath, fname)
        if os.path.isdir(ffile) and ffile != os.path.join(basepath, build_dir) and not fname.startswith('.'):
            for f in getpy(basepath, os.path.join(parentpath, name), fname, build_dir, excepts, copyOther, delC):
                yield f
        elif os.path.isfile(ffile):
            # print("\t", basepath, parentpath, name, ffile)
            ext = os.path.splitext(fname)[1]
            if ext == ".c":
                if delC and os.stat(ffile).st_mtime > starttime:
                    os.remove(ffile)
            elif ffile not in excepts and ext not in('.pyc', '.pyx'):
                # print("\t\t", basepath, parentpath, name, ffile)
                if ext in('.py', '.pyx') and not fname.startswith('__'):
                    yield os.path.join(parentpath, name, fname)
                elif copyOther:
                        dstdir = os.path.join(basepath, build_dir, parentpath, name)
                        if not os.path.isdir(dstdir): os.makedirs(dstdir)
                        shutil.copyfile(ffile, os.path.join(dstdir, fname))
        else:
            pass
 
if __name__ == "__main__":
    currdir = os.path.abspath('.')
    parentpath = sys.argv[1] if len(sys.argv)>1 else "."
 
    currdir, parentpath = os.path.split(currdir if parentpath == "." else os.path.abspath(parentpath))
    build_dir = os.path.join(parentpath, "build")
    build_tmp_dir = os.path.join(build_dir, "temp")
    print("start:", currdir, parentpath, build_dir)
    os.chdir(currdir)
    try:
        #获取py列表
        module_list = list(getpy(basepath=currdir,parentpath=parentpath, build_dir=build_dir, excepts=(setupfile)))
        print(module_list)
        setup(ext_modules = cythonize(module_list),script_args=["build_ext", "-b", build_dir, "-t", build_tmp_dir])
        module_list = list(getpy(basepath=currdir, parentpath=parentpath, build_dir=build_dir, excepts=(setupfile), copyOther=True))
    except Exception as ex:
        print("error! ", ex)
    finally:
        print("cleaning...")
        module_list = list(getpy(basepath=currdir, parentpath=parentpath, build_dir=build_dir, excepts=(setupfile), delC=True))
        if os.path.exists(build_tmp_dir): shutil.rmtree(build_tmp_dir)
 
    print("complate! time:", time.time()-starttime, 's')

思考

这样打包方式一个python文件对应一个so文件。能否将不同的python文件打包到同一个.so文件呢?

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐