cx_freeze打包Python程序

本文介绍如何使用cx_Freeze工具将Python脚本打包成独立的可执行文件,包括安装cx_Freeze的步骤和两种打包方法:命令行方式和使用setup.py文件。通过详细示例,帮助读者掌握Python应用的打包技巧。
该文章已生成可运行项目,

之前用pyinstaller总是报错,每次都要重装以后才能不报错,反正不是这个错就是那个错。自从听说cx_freeze可以打包Python以后,再也不用担心报错啦!

下面是安装cx_freeze的步骤:

1.用pip安装cx_freeze

pip install cx_freeze

2.使用cmd cd到Python目录下的

3.对cx_freeze进行解包,执行下面命令

python  cxfreeze-postinstall

4.检查安装是否成功,执行下面命令

cxfreeze -h

如果返回以下内容,说明安装成功

Usage: cxfreeze [options] [SCRIPT]

Freeze a Python script and all of its referenced modules to a base
executable which can then be distributed without requiring a Python
installation.

Options:
  --version             show program's version number and exit
  -h, --help            show this help message and exit
  -O                    optimize generated bytecode as per PYTHONOPTIMIZE; use
                        -OO in order to remove doc strings
  -c, --compress        compress byte code in zip files
  -s, --silent          suppress all output except warnings and errors
  --base-name=NAME      file on which to base the target file; if the name of
                        the file is not an absolute file name, the
                        subdirectory bases (rooted in the directory in which
                        the freezer is found) will be searched for a file
                        matching the name
  --init-script=NAME    script which will be executed upon startup; if the
                        name of the file is not an absolute file name, the
                        subdirectory initscripts (rooted in the directory in
                        which the cx_Freeze package is found) will be searched
                        for a file matching the name
  --target-dir=DIR, --install-dir=DIR
                        the directory in which to place the target file and
                        any dependent files
  --target-name=NAME    the name of the file to create instead of the base
                        name of the script and the extension of the base
                        binary
  --default-path=DIRS   list of paths separated by the standard path separator
                        for the platform which will be used to initialize
                        sys.path prior to running the module finder
  --include-path=DIRS   list of paths separated by the standard path separator
                        for the platform which will be used to modify sys.path
                        prior to running the module finder
  --replace-paths=DIRECTIVES
                        replace all the paths in modules found in the given
                        paths with the given replacement string; multiple
                        values are separated by the standard path separator
                        and each value is of the form path=replacement_string;
                        path can be * which means all paths not already
                        specified
  --include-modules=NAMES
                        comma separated list of modules to include
  --exclude-modules=NAMES
                        comma separated list of modules to exclude
  --ext-list-file=NAME  name of file in which to place the list of dependent
                        files which were copied into the target directory
  -z SPEC, --zip-include=SPEC
                        name of file to add to the zip file or a specification
                        of the form name=arcname which will specify the
                        archive name to use; multiple --zip-include arguments
                        can be used
  --icon=ICON           name of the icon file for the application

下面是打包方法1步骤(特别简单一行就好)

1.首先cmd cd到需要打包的文件目录下

2.输入打包命令

下面是打包例子

cxfreeze Game.py --target-dir=“C:\Users\Administrator\Desktop\exe\build” --target-name=“Game.exe” --include-modules=“os” --icon=‘1.ico’

说明:
Game.py 是要打包的主文件
–target-dir 是打包后的程序路径
–target-name 是打包后的程序名
–include-modules 是要包含的模块或库
–icon 是打包后的程序图标。

这是我打包用的模板,如果有其他的需要,用检查安装那个命令可以使用其他参数
就是这些

Options:
  --version             show program's version number and exit
  -h, --help            show this help message and exit
  -O                    optimize generated bytecode as per PYTHONOPTIMIZE; use
                        -OO in order to remove doc strings
  -c, --compress        compress byte code in zip files
  -s, --silent          suppress all output except warnings and errors
  --base-name=NAME      file on which to base the target file; if the name of
                        the file is not an absolute file name, the
                        subdirectory bases (rooted in the directory in which
                        the freezer is found) will be searched for a file
                        matching the name
  --init-script=NAME    script which will be executed upon startup; if the
                        name of the file is not an absolute file name, the
                        subdirectory initscripts (rooted in the directory in
                        which the cx_Freeze package is found) will be searched
                        for a file matching the name
  --target-dir=DIR, --install-dir=DIR
                        the directory in which to place the target file and
                        any dependent files
  --target-name=NAME    the name of the file to create instead of the base
                        name of the script and the extension of the base
                        binary
  --default-path=DIRS   list of paths separated by the standard path separator
                        for the platform which will be used to initialize
                        sys.path prior to running the module finder
  --include-path=DIRS   list of paths separated by the standard path separator
                        for the platform which will be used to modify sys.path
                        prior to running the module finder
  --replace-paths=DIRECTIVES
                        replace all the paths in modules found in the given
                        paths with the given replacement string; multiple
                        values are separated by the standard path separator
                        and each value is of the form path=replacement_string;
                        path can be * which means all paths not already
                        specified
  --include-modules=NAMES
                        comma separated list of modules to include
  --exclude-modules=NAMES
                        comma separated list of modules to exclude
  --ext-list-file=NAME  name of file in which to place the list of dependent
                        files which were copied into the target directory
  -z SPEC, --zip-include=SPEC
                        name of file to add to the zip file or a specification
                        of the form name=arcname which will specify the
                        archive name to use; multiple --zip-include arguments
                        can be used
  --icon=ICON           name of the icon file for the application

当然还有使用setup.py程序来设置

下面是打包方法2步骤

1.编写setup.py文件

from cx_Freeze import setup, Executable
 
 
setup(name='Game',
      version = '0.1',
      description='Game file',
      executables = [Executable("Game.py")]
 
      )

这是一个简单的模板

2.还是要先cmd cd到需要打包的程序目录下

3.执行下面命令

python setup.py build

如果想打包成安装包就执行另外一条指令

python setup.py bdist_msi

4.打包好的文件在build文件下

本文章已经生成可运行项目
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值