Python用PyInstaller打包筆記
為了把python程式釋出到沒有安裝python解釋器的Windows環境中使用,需要打包成exe可執行檔案。現在常見的python打包工具有cx_Freeze、PyInstaller和py2exe,想想我當初接觸python的時候,似乎隻有py2exe,而且有不少問題時光荏苒,一切過的真快。本文介紹PyInstaller打包的使用。
一.準備工作
安裝PyWin32
到http://sourceforge.net/projects/pywin32/ 下載下傳 PyWin32,本文使用的是 pywin32-219.win-amd64-py2.7
位址:http://sourceforge.net/projects/pywin32/files/pywin32/Build%20219/pywin32-219.win-amd64-py2.7.exe/download
下載下傳PyInstaller
到http://www.pyinstaller.org/ 下載下傳PyInstaller并解壓縮。本文使用PyInstaller 2.1。
或者使用pip安裝,執行
pip install pyinstaller下載下傳upx(可選)
到http://upx.sourceforge.net/ 下載下傳upx,解壓後把upx放在PyInstaller目錄下,upx的作用是給生成的exe加殼,減小體積。
二.使用方法
cmd切換到PyInstaller檔案夾,執行指令,如:
pyinstaller xxxx.py
當然也可以添加輸出選項,獲得更好的exe可執行檔案,如:
python pyinstaller.py --upx-dir -F xxx.py
-F用于制作獨立的可執行程式,--upx-dir用于壓縮檔案。注意:
- 網上教程常見的-X選項啟用upx已經失效
- 如果upx.exe已經複制到PyInstaller檔案夾下,會預設使用upx,如果不在檔案夾下,可以使用--upx-dir選項,如--upx-dir=upx_dir,如--upx-dir=/usr/local/share/
- 如果upx.exe複制到了PyInstaller檔案夾下,如果不想使用upx,需要添加參數 --noupx
三.常用參數介紹
-F 用于制作獨立的可執行程式
--upx-dir 使用upx加殼進而壓縮exe檔案
--noconsole适用于Windows和Mac OS X,用于建立不顯示控制台視窗的程式
其他
PyInstaller可以用于多個平台的打包,包括 Windows (32-bit and 64-bit),Linux (32-bit and 64-bit),Mac OS X (32-bit and 64-bit),experimentally Solaris and AIX。
PyInstaller也可以自定義ico檔案等,完整使用手冊參見http://pythonhosted.org/PyInstaller/