admin 發表於 2022-4-11 21:50:35

如何將Python 打包成exe執行檔

第一步:安裝pyinstallerpip install pyinstaller
平均動脈壓計算程式 MAP.pysp = int(input("請輸入收縮壓:"))
dp = int(input("請輸入舒張壓:"))

MAP = (sp + (2 * dp)) / 3

if(MAP>=70 and MAP <=120):
    print("平均動脈壓為",MAP,"mmHg")
    print("為正常範圍")
else:
    print("平均動脈壓為",MAP,"mmHg")
    print("此數值須注意")
pyinstall 常用參數介紹這邊列出一些 pyinstaller 常用的參數,基本上小程式用到這些就夠了。

[*]-F 包裝成單一檔案
[*]-D 包裝成相依性資料夾
[*]-c 僅使用命令列,不開啟視窗
[*]-w 僅使用使用者圖形介面,不開啟命令列
[*]--icon=PATH 設定 icon
開始封裝常用參數了解後我們就直接開始封裝吧,平均動脈壓計算機需要包裝成單一檔案,以命令列執行,且指定 icon 為 heart.ico,那麼包裝指令就是這樣:
pyinstaller -F MAP.py -c --icon=heart.ico一執行完他就會自動開始包裝 EXE 檔,等他一下就會完成囉~ 會生成一些資料夾跟檔案,而我們的執行檔被存在 dist 資料夾中。

https://ithelp.ithome.com.tw/upload/images/20191011/20120282icTkTP5PEw.png
https://ithelp.ithome.com.tw/upload/images/20191011/20120282VDCosGEaEO.png

從此以後只要那個 EXE 就可以在 windows x64 電腦上執行,不需要額外安裝 Python 與 Pyinstaller,對於程式的發表相當方便,軟體執行結果也展示一下。
https://ithelp.ithome.com.tw/upload/images/20191011/20120282FrEWt6uCEh.png

頁: [1]
查看完整版本: 如何將Python 打包成exe執行檔