cx_Freeze將py檔案打包成exe檔案,隱藏控制檯
阿新 • • 發佈:2019-02-15
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"- 只能指定一個要打包的模組,也就是啟動模組
- 所有.py檔案都不能有中文字元,否則會出現編碼異常。
-
釋出後,可執行檔案執行路徑不能有中文(最好也不要有空格)。
- 啟動執行的檔案中不要有下面這種判斷,否則可執行檔案執行會沒有任何效果。
if __name__ == "__main__":
main()
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