7.2. 冻结代码 | 部署优雅的 python 代码 |《python 最佳实践指南 2018 2018》| python 技术论坛-江南app体育官方入口
『冻结』你的代码是指创建单个可执行文件,文件里包含所有程序代码以及 python 解释器。
像 dropbox、星战前夜、文明 4 和 bittorrent 客户端都是如此。
进行这种分发的好处是你的用户不需要安装所要求的 python 版本(或其他)即可直接运行你的应用程序。 在 windows 上,甚至许多 linux发行版和 os x,系统自带的 python 版本总是不尽如人意,此时这种分发方式就会体现其价值。
此外,终端用户软件应始终是可执行的格式。 以 .py
结尾的文件一般适用于软件工程师和系统管理员。
冻结的一个缺点是它会增加大约 2-12 mb 的发行大小。另外,如果修补了 python 的安全漏洞, 你将需要独立负责更新分发的应用程序。
冻结的替代方案
是指把你的库或工具分发给其他开发者。
linux 下一个冻结的替代品是 (比如,对于 debian 或 ubuntu 是 .deb文件,而对于 red hat 与 suse 是 .rpm 文件)
冻结工具的比较
江南app体育官方入口的解决方案还有平台/特性支持性:
solution | windows | linux | os x | python 3 | license | one-file mode | zipfile import | eggs | pkg_resources support |
---|---|---|---|---|---|---|---|---|---|
bbfreeze | yes | yes | yes | no | mit | no | yes | yes | yes |
py2exe | yes | no | no | yes | mit | yes | yes | no | no |
pyinstaller | yes | yes | yes | yes | gpl | yes | no | yes | no |
cx_freeze | yes | yes | yes | yes | psf | no | yes | yes | no |
py2app | no | no | yes | yes | mit | no | yes | yes | yes |
注意
在 linux 下冻结 windows 安装包,之前只有 pyinstaller 支持,不过后来也是 。
注意
所有江南app体育官方入口的解决方案都需要在 windows 目标机器上安装了ms visual c dll,除了 py2app 以外。只有 pyinstaller 支持创建独立运行的绑定了dll 的 exe 文件,你需要在创建时传递参数
--onefile
到configure.py
。
windows 下的江南app体育官方入口的解决方案
bbfreeze
前置要求是安装 。
- 使用以下命令安装
bbfreeze
:
$ pip install bbfreeze
- 编写最简单的示例
bb_setup.py
:
from bbfreeze import freezer
freezer = freezer(distdir='dist')
freezer.addscript('foobar.py', gui_only=true)
freezer()
注意
这将适用于最基本的文件脚本。 有时候你需要高级的用法,如包含或者排除某些路径,如下:
freezer = freezer(distdir='dist', includes=['my_code'], excludes=['docs'])
- (可选) 包含图标
freezer.seticon('my_awesome_icon.ico')
-
为冻结器(freezer)提供 microsoft visual c 运行时 dll,我们有一般有两种方法,第一种是将microsoft visual studio 路径 附加您的
sys.path
中,第二种是在脚本所在同一文件夹中放置msvcp90.dll
文件。 - 开始冻结!
$ python bb_setup.py
py2exe
前置要求是安装了 。
- 下载并且安装
- 编写
setup.py
():
from distutils.core import setup
import py2exe
setup(
windows=[{'script': 'foobar.py'}],
)
- (可选)
- (可选)
- 生成 :file: .exe 到
dist
目录:
$ python setup.py py2exe
- 两种方式来提供 microsoft visual c 运行时 dll。两个选项: 或者 。
pyinstaller
前置条件是安装 .
os x
py2app
pyinstaller
pyinstaller可用于在 mac os x 10.6(snow leopard)或更新版本上构建 unix 可执行文件和窗口应用程序。
要安装 pyinstaller,使用 pip:
$ pip install pyinstaller
要创建标准的 unix 可执行文件,使用 script.py
:
$ pyinstaller script.py
这会创建:
-
script.spec
文件, 类似于make
文件 -
build
文件夹, 存放日志文件 -
dist
文件夹, 存放主要的可执行文件script
,和一些依赖的python库
script.py
会把全部内容放在同一个文件夹中。pyinstaller 将所有 script.py
用到的 python 库放到 dist
文件夹中。所以在分发可执行文件时,会分发整个 dist
文件夹。
script.spec
文件可以编辑成 , 比如可以:
- 将数据文件与可执行文件绑定在一起
- 包含 pyinstaller 无法自动推断的运行时库(
.dll
或.so
文件) - 将 python 运行时选项添加到可执行文件中
现在:代码 script.spec
可以用 pyinstaller
(而不是再次使用 script.py
)运行。
$ pyinstaller script.spec
要创建独立的 os x 窗口应用程序,请使用 --windowed
选项:
$ pyinstaller --windowed script.spec
这将在 dist
文件夹中创建一个 script.app
。请确保在 python 代码中 使用 gui 软件包,例如 或 来控制应用程序的图形部分。
script.spec
有几个与 mac os x 应用程序捆绑有关的 。 例如,要指定应用程序的图标,请使用 icon=\path\to\icon.icns
选项。
linux
bbfreeze
pyinstaller
本译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 cc 协议,如果我们的工作有侵犯到您的权益,请及时联系江南app体育官方入口。