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

發現已經安裝好了,進入Python37\Lib\site-packages\路徑發現有2個文件

Python37\Scripts路徑下有

打開CMD,cd 到Python37\Scripts路徑下運行

 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文件都不能有中文字符,否則會出現編碼異常。完……

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM