利用PyInstaller將python程式.py轉為.exe的方法詳解,pyinstaller.py
前言
最近經常用到一個.py程式,但是每次在不同電腦上用,希望能把Python指令碼發布為脫離Python平台啟動並執行可執行程式,比如單個exe。PyInstalle滿足要求。
PyInstaller本身並不屬於Python包。在安裝 pyinstaller之前需把python環境配置好。
安裝pyinstaller
下載pyinstaller
解壓到F:\PyInstaller-2.1(自選)(可以去官網下載最新版)
安裝pywin32
pywin32-217.win32-py2.7.exe:點擊下載
安裝pyinstaller
1、進入cmd
cd F:\PyInstaller-2.1
python pyinstaller.py --console --onefile test.py
如果提示:
Usage: python pyinstaller.py [opts] [ …] | pyinstaller.py: error: Requires at least one scriptname file or exactly one .spec-file
則說明安裝完成了。
測試打包
1、檔案放在目前的目錄的pyinstaller-2.1檔案夾裡面
cd F:\PyInstaller-2.1
python pyinstaller.py --console --onefile test.py
2、命令運行成功後會產生一個test檔案夾。在這個檔案夾下面會有一個名為dist的檔案夾,此檔案夾下面有轉換好的test.exe
3、上面編譯出來的exe能夠正常運行了,但帶一個黑色的console,以下重新編譯,加入–windowed –icon,取消–console
python pyinstaller.py -w --onefile --icon="my.ico" test.py
其中my.ico是你要給他加的自訂表徵圖檔案。
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家學習或者使用python能帶來一定的協助,如果有疑問大家可以留言交流,謝謝大家對幫客之家的支援。