Python 打包可执行文件

发布于 2020-11-08  303 次阅读


综述

Python 可以将 代码编译打包成 可执行文件进行发布。

详细内容

freeze python codes

SolutionWindowsLinuxOS XPython 3LicenseOne-file modeZipfileimport Eggspkg_resources supportLatest release dat
bbFreezeyesyesyesnoMITnoyesyesyesJan 20, 2014
py2exeyesnonoyesMITyesyesnonoOct 21, 2014
pyInstalleryesyesyesyesGPLyesnoyesnoJul 9, 2019
cx_FreezeyesyesyesyesPSFnoyesyesnoAug 29, 2019
py2appnonoyesyesMITnoyesyesyesMar 25, 2019

用于 python 打包的工具如上表所示,考虑到跨平台,因此考虑使用 pyinstaller

pyinstaller

原理

pyinstaller 会根据输入的 xx.py 分析创建打包配置脚本 xx.spec
然后根据 xx.spec 分析依赖打包文件

# win
pyinstaller --add-data='src;dst' --distpath='distpath' test.py

# 等同于
pyi-makespec --add-data='src;dst' test.py
pyinstaller --disptpath='distpath' test.spec

朝闻道,夕死可矣