cx_Freeze可以把Python程序打包成可执行文件,跟py2exe和py2app一样。但是cx_Freeze是跨平台的,可以运行Python的平台,就可以使用cx_Freeze。支持Python 2.6以上的版本,包括Python 3。
一、安装
最新的发布版本是2014年11月6号发布的4.3.4。
1.使用命令安装:pip install cx_Freeze
2.从PyPI下载。
二、使用
有三种使用方式:
1.创建一个distutils setup脚本。如果打包时需要额外的选项,推荐这种方式,因为会在脚本中保存打包时的选项。运行cxfreeze-quickstart可以生成一个简单的setup脚本。
2.使用cxfreeze script。
3.直接使用cx_Freeze提供的类和模块。保留这种方式是为了复杂的脚本、更方便的扩展和嵌入式系统。
一般来说,cx_Freeze生成一个包含可执行文件的目录, 其中包括需要的动态链接库(DLLs或.so文件)。
使用setup脚本的bdist_msi可以生成一个简单的Windows安装程序,使用bdist_dmg生一个Mac下的硬盘镜像。想要创建高级的Windows安装程序,可以使用第三方工具,例如Inno Setup,打包cx_Freeze生成的文件。
可执行文件用到的Python模块存放在zip文件中,可以有三种方式存放:
1.默认创建一个library.zip的压缩文件,其中包括所有的模块。
2.每个可执行文件可以有一个私有的zip文件,与可执行文件同名(不包括.zip后缀)。
3.每个可执行文件有一个包括模块的zip文件。cx_Freeze的早期版本默认是这种方式,但是不能创建RPM,因为RPM构造器移除了可执行文件。
三、distutils setup脚本
要想使用distutils,需要创建一个setup脚本。文件名通常是setup.py,当然也可以是别的名字。如下:
import sys
from cx_Freeze import setup, Executable
# 自动检测依赖项,但有时需要手动添加
build_exe_options = {"packages": ["os"], "excludes": ["tkinter"]}
# GUI 程序需要不同的base,默认是控制台程序
base = Noneif 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)])
setup(...
options = {'build_exe': {'init_script':'Console'}})
option name | description |
---|---|
build_exe (-b) | 创建的exe文件和依赖文件的目录,默认为 build/ |
option name | description |
---|---|
build_exe (-b) | 创建的exe文件和依赖文件的目录,默认为 build/ |
optimize (-o) | 优化级别,0 (不优化), 1或 2中的一个 |
excludes (-e) | 用逗号分隔排除的模块名称列表 |
includes (-e) | 用逗号分隔包含的模块名称列表 |
packages (-p) | 用逗号分隔包含的包名列表,包括了包中的所有子模块 |
namespace_packages | comma separated list of packages to be treated as namespace packages (path is extended using pkgutil) |
replace_paths | Modify filenames attached to code objects, which appear in tracebacks. Pass a comma separated list of paths in the form |
path | 用逗号分隔搜索路径列表,默认值为sys.path |
compressed (-c) | 创建一个压缩的zip文件 |
constants | comma separated list of constant values to include in the constants module called BUILD_CONSTANTS in form |
include_files | 需要拷贝到目标文件夹的文件列表,列表包含字符串,或者源与目标的元组;源可能是文件或文件夹(包括除了.svn和CVS的所有子目录);目标不能是绝对路径 |
include_msvcr | 包括Microsoft的C运行库;如果运行可执行文件需要manifest文件,就不需要重新发布 |
zip_includes |
包括在zip文件中的文件名列表;列表包含字符串,或者源与目标的元组
|
bin_includes | 通常情况下不会被加入依赖项的二进制文件列表;文件名后的版本号会被忽略 |
bin_excludes | 不希望加入依赖项的二进制文件列表;文件名后的版本号会被忽略 |
bin_path_includes | 加入依赖项的二进制文件的路径类别 |
bin_path_excludes | 不希望加入依赖项的二进制文件的路径列表 |
silent (-s) | 禁止除警告之外的所有输出 |
option name | description |
---|---|
install_exe | 安装后可执行文件和依赖文件的目录 |
option name | description |
---|---|
install_dir (-d) | 可执行文件的安装目录;在Windows下默认是"Program Files"目录下 |
build_dir (-b) | 构建目录(从哪安装);默认是build命令的build_dir |
force (-f) | 强制安装,覆盖已经存在的文件 |
skip_build | 跳过build的步骤 |
option name | description |
---|---|
add_to_path | 添加目标文件夹到PATH环境变量;如果执行文件中有控制台程序,默认值为True,否则为False |
upgrade_code | 为创建的包定义升级码;用来强制移除之前使用同一个升级码创建的所有包 |
option name | description |
---|---|
iconfile | Path to an icns icon file for the application. This will be copied into the bundle. |
qt_menu_nib | Path to the qt-menu.nib file for Qt applications. By default, it will be auto-detected. |
bundle_name | File name for the bundle application without the .app extension. |
custom_info_plist | File to be used as the Info.plist in the app bundle. A basic one will be generated by default. |
include_frameworks | A list of Framework directories to include in the app bundle. |
codesign_identity | The identity of the key to be used to sign the app bundle. |
codesign_entitlements | The path to an entitlements file to use for your application’s code signature. |
codesign_deep | Boolean for whether to codesign using the –deep option. |
codesign_resource_rules | Plist file to be passed to codesign’s –resource-rules option. |
option name | description |
---|---|
volume_label | Volume label of the DMG disk image |
applications-shortcut | Boolean for whether to include shortcut to Applications in the DMG disk image |
argument name | description |
---|---|
script | 包含创建脚本的文件名 |
initScript | the name of the initialization script that will be executed before the actual script is executed; this script is used to set up the environment for the executable; if a name is given without an absolute path the names of files in the initscripts subdirectory of the cx_Freeze package is searched |
base | the name of the base executable; if a name is given without an absolute path the names of files in the bases subdirectory of the cx_Freeze package is searched |
path | list of paths to search for modules |
targetDir | the directory in which to place the target executable and any dependent files |
targetName | the name of the target executable; the default value is the name of the script with the extension exchanged with the extension for the base executable |
includes | list of names of modules to include |
excludes | list of names of modules to exclude |
packages | list of names of packages to include, including all of the package’s submodules |
replacePaths | Modify filenames attached to code objects, which appear in tracebacks. Pass a list of 2-tuples containing paths to search for and corresponding replacement values. A search for ‘*’ will match the directory containing the entire package, leaving just the relative path to the module. |
compress | boolean value indicating if the module bytecode should be compressed or not |
icon | name of icon which should be included in the executable itself on Windows or placed in the target directory for other platforms |
namespacePackages | list of packages to be treated as namespace packages (path is extended using pkgutil) |
shortcutName | the name to give a shortcut for the executable when included in an MSI package |
shortcutDir | the directory in which to place the shortcut when being installed by an MSI package; see the MSI Shortcut table documentation for more information on what values can be placed here. |
def find_data_file(filename):
if getattr(sys, 'frozen', False):
# The application is frozen
datadir = os.path.dirname(sys.executable)
else:
# The application is not frozen
# Change this bit to match where you store your data files:
datadir = os.path.dirname(__file__)
return os.path.join(datadir, filename)