用Pyinstaller 打包 Python 生成 Mac OS X 應用

安裝

pip3 install pyinstaller

然後在 terminal 中 cd 到項目路徑,
運行程序入口,也就是main函數所在文件 main.py
在項目所在工作路徑輸入命令:

sudo pyinstaller --windowed --onefile --clean --noconfirm main.py
sudo pyinstaller --clean --noconfirm --windowed --onefile main.spec

sudo 獲取權限,可能要求輸入密碼
如果想要打出來的包有圖標第一條命了替換:

pyinstaller --windowed --onefile --icon=sat_tool_icon.icns --clean --noconfirm main.py

一般很少一次通過,主要是一些包導入問題和項目文件配置路徑
我遇到問題時找不到項目的配置文件
獲取包路徑的父路徑,不可將路徑寫成絕對路徑

os.path.dirname(sys.argv[0])

打出來的包是:
包展示
點擊wx_main時候 我遇到了 This program needs access to the screen. Please run with a
Framework build of python, and only when you are logged in
on the main display of your Mac.沒找到好的解決方法,然後採用點擊包裏面的可執行文件顯示的,具體操作如下:
顯示wx_main_file包內容,會看到MacOS文件,我將配置文件放入
在這裏插入圖片描述
然後點擊wx_main運行,終端可以看到打印的日誌信息

由於是一個小工具就沒有打包成 .dmg文件

參考文章

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章