之前用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