python cx_Freeze安装详解、打包exe文件简解

最近想打包自己写的程序,前面用了Python pyinstaller,但是总会出现其他目录的.py文件找不到的问题,弄了一天,放弃了。现在试试Python cx_freeze。

  1. 安装:

pip install cx-freeze 安装。在安装时出现 error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools":……

查看资料要去https://www.lfd.uci.edu/~gohlke/pythonlibs/下载对应的Microsoft Visual C++ 14.0

下载对应的版本,cp37的意思是python37版(我用的版本)安装。

pip install D:Program Files下载cx_Freeze-5.1.1-cp37-cp37m-win_amd64.whl

然后运行

pip install cx_freeze

发现已经安装好了,进入Python37Libsite-packages路径发现有2个文件

Python37Scripts路径下有

打开CMD,cd 到Python37Scripts路径下运行

 python cxfreeze-postinstall

这会安装成功了吧!

cmd 运行

cxfreeze -h

失败!

'C:Program' 不是内部或外部命令,也不是可运行的程序
或批处理文件。

后来查看资料,发现要修改上图中的cxfreeze.bat文件,文件中就是python3以及cx_freeze的路径,启动python3,运行cx_freeze,并回显到控制台上,我发现我的python3安装在C:Program Files路径下,而.bat文件不能读取空格(坑……),最后将路径用引号夸起来保存。

在次运行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
……
……
……

这个显示OK!

  2.打包

运行到脚本路径下

cxfreeze main.py --target-dir dist

在dist目录下生成这4个文件,将main.exe运行,闪退……

最后发现所有.py文件都不能有中文字符,否则会出现编码异常。完……

原文地址:https://www.cnblogs.com/gexbooks/p/11270206.html