使用cx_Freeze打包Python程序

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)])

运行命令:python setup.py build
执行完成后,当前文件夹下会生成一个build文件夹,里面有一个以exe.开头,以当前平台标识结尾的文件夹。
运行命令:python setup.py bdist_msi
在Mac OS下,使用bist_dmg选项。

在Windows下,可以创建一个简单的安装程序,包括所有了所有文件。
运行命令:python setup.py bdist_msi
在Mac OS下,使用bist_dmg选项。

cx_Freeze创建了四个新的命令和子类,用来构建和安装可执行程序。可以用在setup脚本、命令行或者setup.cfg中。
在setup.py中使用下划线指定选项:
setup(...
          options = {'build_exe': {'init_script':'Console'}})
在命令行中使用破折号指定选项:
     python setup.py build_exe --init-script Console
有些选项可以在命令行中使用缩写,见在下面的括号中。

1、build
这是一个标准命令,cx_Freeze修改成用来构建任何可执行文件。下面的选项被添加到标准选项集中:
运行命令:python setup.py bdist_msi
在Mac OS下,使用bist_dmg选项。

在Windows下,可以创建一个简单的安装程序,包括所有了所有文件。
option name description
build_exe (-b) 创建的exe文件和依赖文件的目录,默认为 build/
2、build_exe
这个命令创建一个或多个可执行文件。可以自定义:
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 =. The value * in the search portion will match the directory containing the entire package, leaving just the relative path to the module.
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) 禁止除警告之外的所有输出
3、install
安装所有定义的exe文件。下面的选项被添加到标准选项集中:
option name description
install_exe 安装后可执行文件和依赖文件的目录
4、install_exe
这个命令安装一个或多个可执行文件。可以被直接使用,但是更多的时候用在构建Windows安装程序或者RPM包。可以自定义:
option name description
install_dir (-d) 可执行文件的安装目录;在Windows下默认是"Program Files"目录下-的子目录,别的平台默认是/lib;除了Windows之外的平台,还会为每个可执行文件创建/bin
build_dir (-b) 构建目录(从哪安装);默认是build命令的build_dir
force (-f) 强制安装,覆盖已经存在的文件
skip_build 跳过build的步骤
5、bdist_msi
这个命令在Python 2.5和更高版本中是标准命令,被cx_Freeze修改成处理安装执行文件和依赖文件。 下面的选项被添加到标准集中:
option name description
add_to_path 添加目标文件夹到PATH环境变量;如果执行文件中有控制台程序,默认值为True,否则为False
upgrade_code 为创建的包定义升级码;用来强制移除之前使用同一个升级码创建的所有包

6、bdist_rpm
这是一个标准命令,被cx_Freeze修改成确认所有包的创建都适合当前平台架构。 这个标准命令假设,如果没有扩展模块,包将会被独立的构建。

7、bdist_mac
这个命令用在Mac OS操作系统上,创建一个Mac的程序bundle(一个.app目录)
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.
8、bdist_dmg
这个命令用在Mac OS操作系统上,创建一个Mac的程序bundle,并打包成一个DMG硬盘镜像,用来发布和安装。
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

9、cx_Freeze.Executable
build_exe命令的选项默认适用于所有创建的可执行文件。Executable类的选项可以指定特定的可执行文件的值。 构造函数的参数如下:
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.

四、使用cxfreeze脚本
cxfreeze脚本与其它脚本都包含Python的安装目录。 Windows和Mac平台下,在Python安装目录的Script子目录中,Unix平台在Python安装目录的bin前缀目录中。
假设现在有一个hello.py文件,可以这样使用cxfreeze脚本:
cxfreeze hello.py --target-dir dist

五、问题
1、不能运行打包后的可执行文件
普遍问题是cx_Freeze没有自动检测到需要拷贝的文件。 代码引入的模块可以检测到,但是动态导出的文件,例如插件系统,需要显示告诉cx_Freeze。
在setup.py中很容易做到:
1)如果是Python代码,在package选项中指定模块名
2)在include-files选项中指定库文件(dll或者so文件)
3)数据文件有点复杂,查看 使用数据文件

2、使用数据文件
程序经常需要数据文件,例如图标。使用setup.py,可以把数据文件或文件夹在build_exe中的include_files中列出来。 数据文件会被拷贝到可执行文件目录。然后使用下面的代码找到数据文件:
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)
另一个替代方案是把数据文件打包到程序中,查看 使用Qt资源系统。

3、Microsoft  Visual C++  Redistributable Package
在Windows下,Python需要Microsoft Visual C++ Redistributable Package。Python2.6-3.2使用2008版本。 因为安装方式不一样,也不清楚是不是每个人都有正确的DLLs,cx_Freeze不能自动的拷贝到应用程序中。
需要检测许可证,相关的DLLs能否被拷贝
1)如果能被拷贝,在build_exe的include_msvcr中指定,让它们自动发布。
2)如果不能,客户需要安装Microsoft  Visual C++  Redistributable Package(微软免费下载)。 SP1版本的不行,需要匹配Python的版本。
2008(Python2.6-3.2)
2010(Python3.3)
直到Python2.5,然后从3.3开始,MSVCR DLLs安装在正常位置,cx_Freeze会自动拷贝。

4、单个可执行文件
cx_Freeze不支持把所有库文件打包成一个单独的可执行文件。

5、打包exe后,运行出现The camera service is missing或者no service found for org.qt-project.qt.camera错误
把python安装目录下\\Lib\\site-packages\PyQt5\plugins中的mediaservice文件夹拷贝到exe所在目录。

你可能感兴趣的:(Python)