Python 可以透過 PyInstaller 將執行檔編譯成 Windows 可以使用的 exe,很好用,不同的 python 版本編譯出來的 exe 對目前的 Windows 有不同的支援度:
Python 版本 | Windows 7 | Windows 8 | Windows 10 | Windows 11 |
---|---|---|---|---|
Python 3.6 | ✅ (最後支援) | ✅ | ✅ | ✅ |
Python 3.7 | ✅ | ✅ | ✅ | ✅ |
Python 3.8 | ✅ | ✅ | ✅ | ✅ |
Python 3.9 | ✅ | ✅ | ✅ | ✅ |
Python 3.10 | ⚠️ (可能有問題) | ⚠️ (可能有問題) | ✅ | ✅ |
Python 3.11 | ❌ (不支援) | ❌ (不支援) | ✅ | ✅ |
Python 3.12 | ❌ (不支援) | ❌ (不支援) | ✅ | ✅ |
所以編譯的時候要考量 python 的版本,Pyinstaller 只有支援 Windows,在 Linux like 的環境要執行必須透過 Wine 這種指令驅動,如果你要跑 CI/CD 沒那麼方便,後來我選擇了 tobix/pywine - Docker Image | Docker Hub 這個 docker 專案,只要選擇你要的 python 版本,如 tobix/pywine:3.8
,將外部的 python 檔案 mount 進去 container,執行 docker exec -it builder wine pyinstaller.exe -F xxx.py
便可以編譯了,如果有相關要安裝的套件,也可以事先執行 docker exec -it builder wine python.exe -m pip install -r /tmp/requirements.txt
。