標籤:
轉自http://www.itoldme.net/archives/1242
為了把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 myscript.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/
轉:Python用PyInstaller打包筆記