1. 程式人生 > >cx_Freeze將py檔案打包成exe檔案,隱藏控制檯

cx_Freeze將py檔案打包成exe檔案,隱藏控制檯

docs:http://cx-freeze.readthedocs.io/en/latest/distutils.html

download:https://pypi.python.org/pypi/cx_Freeze/

在python中比較常用的python轉exe方法有三種,分別是cx_freeze,py2exe,PyInstaller。py2exe恐怕是三者裡面知名度最高的一個,但是同時相對來說它的打包質量恐怕也是最差的一個。pyinstaller打包很好,但是操作工序較為複雜。

cx_freeze打包的exe需要和依賴庫放在一個資料夾

第一步:檢查cx_freeze是否安裝正確。

進入Python的Scripts資料夾

>cxfreeze -h

顯示用法,則安裝正確

第二步:如果安裝正確,那麼接下來的事情就非常簡單了

正式開始打包,命令為: cxfreeze hello.py --target-dir dist 加上紅色引數可以隱藏控制檯 cxfreeze hello.py   --base-name="win32gui"

命令解釋:hello.py 是你要打包的主檔案、啟動檔案

Dist為要目標資料夾,打包後會生成dist目錄,裡面就有打包後的可執行檔案。

注意:

  1. 只能指定一個要打包的模組,也就是啟動模組
  2. 所有.py檔案都不能有中文字元,否則會出現編碼異常。
  3. 釋出後,可執行檔案執行路徑不能有中文(最好也不要有空格)。
  4. 啟動執行的檔案中不要有下面這種判斷,否則可執行檔案執行會沒有任何效果。 
    if __name__ == "__main__": 
    main()
2,用setup.py安裝
import sys
from cx_Freeze import setup, Executable

# Dependencies are automatically detected, but it might need fine tuning.
build_exe_options = {"packages": ["os"], "excludes": ["tkinter"]}

# GUI applications require a different base on Windows (the default is for a
# console application).
base = None
if sys.platform == "win32":
    base = "Win32GUI"

setup(  name = "gui",
        version = "8.1",
        description = "application!",
        options = {"build_exe": build_exe_options},
        executables = [Executable("fun.py", base=base, targetName = 'launch.exe',icon = "qq.ico")])


出處:http://keliang.blog.51cto.com/3359430/661884