###### tags: `python` # py2exe 打包成執行檔 ## 安裝 pip install cx_Freeze ## 執行 1. 建立專案資料夾,例如test,以下均在專案資料夾中儲存/執行! 2. 撰寫python程式檔(例如test.py) 3. 撰寫setup.py 4. 點「開始」,輸入cmd 5. cd Desktop/test 5. python setup.py bdist_msi 6. cd dist 7. 內部msi就是安裝檔 ## setup.py範例一 ```python # -*- coding:utf-8 -*- from cx_Freeze import setup, Executable pyfile = "test.py" base = None # 一般程式 options = { 'build_exe': { 'packages':[], 'include_files': [] }, } setup( name = "hello", options = options, version = "1.0", description = 'my first exe', executables = [Executable(pyfile, base=base)], ) ``` ## 範例二 - 圖形化程式 ```python= # -*- coding:utf-8 -*- from cx_Freeze import setup, Executable pyfile = "GUI1.py" base = "Win32GUI" # 視窗程式 options = { 'build_exe': { 'packages':[], }, } setup( name = "hello", options = options, version = "1.0", description = '猜大小遊戲', executables = [Executable(pyfile, base=base)] ) ``` ## 補充說明 * python setup.py build: 產生命令列程式檔 * python setup.py bdist_msi: 產生安裝程式檔 ## 參考資料 * [cx_Freeze官網](http://cx-freeze.readthedocs.io/en/latest/index.html)