新网站多久被百度收录,图片编辑工具免费版,房屋设计图平面图,国内网站备案流程一、Cpython
Python是一门解释型语言#xff0c;当我们想让其他人运行我们的代码时#xff0c;如果直接将.py源代码发送给他人#xff0c;那么源代码将没有任何安全性可言#xff0c;也就是任何一个人都可以打开源代码一看究竟#xff0c;任何人都可以随意修改源代码。
…一、Cpython
Python是一门解释型语言当我们想让其他人运行我们的代码时如果直接将.py源代码发送给他人那么源代码将没有任何安全性可言也就是任何一个人都可以打开源代码一看究竟任何人都可以随意修改源代码。
而为了防止源代码泄露可以将Python源代码编译生成.pyd库文件或者.so库文件Windows平台生成pyd文件Linux生成so文件。
1.1 Python有以下几种类型的文件
pyPython控制台程序的源代码文件 pywPython带用户界面的源代码文件 pyxPython包源文件 pycPython字节码文件可通过逆向编译来得到源码 pyoPython优化后的字节码文件可通过逆向编译来得到源码 pyd在Windows平台上Python的库文件Python版DLL so在Linux平台上Python的库文件是so文件
1.2 使用Cpython编译项目步骤
example: hello.py
def say_hello():print(Hello!)
1.2.1 安装Cpython
pip3 install Cython
1.2.2 编写转换文件
setup.py
from setuptools import setup
from Cython.Build import cythonize# python3 setup.py build_ext --inplace# 所有需要编译的py文件
all_py_file [hello.py]setup(namehello,ext_modulescythonize(all_py_file),version1.0,authorLeo,author_emailLeoLi.Ligroupm.com
)1.2.3 执行转换生成so文件
进入目录
执行python3 setup.py build_ext --inplace
1.2.4 测试编译好的so文件
将py文件等相关编译文件删除
测试是否可以正常导入并使用hello.py的函数
1. 3yd/so文件反编译
pyd/so文件是由 Cython首先把python源码翻译成了 .c文件(这个过程基本不可逆)再把这个.c文件编译成了pyd/so文件。
二、 Pyinstaller
pyinstaller是一个第三方库它能够在Windows、Linux、 Mac OS X 等操作系统下将 Python 源文件打包通过对源文件打包 Python 程序可以在没有安装 Python 的环境中运行也可以作为一个 独立文件方便传递和管理。
Pyinstaller打包的可执行文件并不是跨平台的而是希望在哪个平台上运行就需要在哪个平台上进行打包。 安装pyinstaller: python3 -m pip install --no-cache-dir pyinstaller -i https://mirrors.aliyun.com/pypi/simple/;
2.1 使用pyinstaller打包 py文件
项目目录
main.py
from hello import say_helloif __name__ __main__:
say_hello()
打包
pyinstaller --name say_hello --onedir --log-level WARN --strip --paths /leo/gme/pyinstaller_demo --distpath /leo/gme/pyinstaller_demo/package /leo/gme/pyinstaller_demo/main.py
执行打包后的可执行文件
2.2 使用pyinstaller打包 Cpython编译后的so
Cpython可以将py编译成so文件将编译好的so文件以原来的工程组织形式(module)存放好注意module下要有非编译的__init__.py, 工程的main.py也不要编译
pyinstaller的打包过程会从main.py走一遍所有调用的module并打包进去但是编译好的pyd不会被识别import这就是为什么要保留原来module的__init__.py, 对于这些已经编译为so的module属于隐式import需要在打包时加入–hidden-import
项目目录
main.py
from hello import say_helloif __name__ __main__:say_hello()
打包
pyinstaller --hidden-import hello --name say_hello --onedir --log-level WARN --strip --paths /leo/gme/pyinstaller_demo --distpath /leo/gme/pyinstaller_demo/package /leo/gme/pyinstaller_demo/main.py
执行打包后的可执行文件
_internal 依赖的是so文件
三、参考资料
Cpython编译https://www.cnblogs.com/gcgc/p/16529975.html Pyinstaller介绍https://blog.csdn.net/weixin_45953322/article/details/128774685 Pyinstaller打包so文件 https://blog.csdn.net/weixin_39916966/article/details/130781599