Python打包cx_Freeze工具使用

cx_Freeze

cx_Freeze构建脚本

cx_Freeze建立在构建脚本(官方假设其名为setup.py),其基本格式为:

import sys
from cx_Freeze import setup, Executable

# Dependencies are automatically detected, but it might need fine tuning.
build_exe_options = {"packages": ["os"], "excludes": ["tkinter"]}

# GUI applications require a different base on Windows (the default is for a
# console application).
base = None
if sys.platform == "win32":
    base = "Win32GUI"

setup(  name = "guifoo",
        version = "0.1",
        description = "My GUI application!",
        options = {"build_exe": build_exe_options},
        executables = [Executable("guifoo.py", base=base)])

在命令行中使用命令:

python setup.py build

该命令将在setup.py脚本同目录下生成一个 build 文件夹,其中包含了 setup.py中指定名称的可执行文件,同时包含了其他依赖的库等。
在Windows下可以使用如下指令生成安装文件(msi):

python setup.py bdist_msi

而在Mac OS X中,则可以使用如下指令生成对应的dmg安装文件:

python setup.py bdist_dmg

官方提供了若干依赖脚本示例 — Github地址
关于cx_Freeze中setup.py文件里的相关参数说明,可直接参考 官方文档,后续本文可能会对部分特殊的情况进行补充说明

cx_Freeze脚本异常情况补充说明

部分异常及相应问题解决可参考如下链接:
用 cx_Freeze 将 Python 脚本编译为 Windows exe 实战

发布了84 篇原创文章 · 获赞 29 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/qq_39852676/article/details/101532387