pyinstaller打包exe檔案的樣本

來源:互聯網
上載者:User

本篇算是 python編寫windows tomcat守護進程 篇的延續 。之前寫的代碼沒事想要打包成exe檔案,可以將python打包成exe的工具主要有py2exe、pyinstaller、cx_freeze 。其中py2exe感覺是最難用,打包最爛的;cx_freeze打包比較簡單,但是功能沒有另外兩個強大,所以個人感覺pyinstaller是最好用的。pyinstaller支援打包壓縮、打包成單個檔案、增加個性表徵圖、設定著作權資訊。效果如下:

一、安裝

pyinstaller安裝比較簡單,支援pip 安裝,直接使用如下命令即可完成安裝:

pip install pyinstaller
不怕麻煩的也可以通過下載源碼python setup.py install 進行安裝 。

需要注意的是,pyinstaller在windows平台下依賴pywin32模組。在使用pyinstaller前,需要安裝好pywin32模組(exe 的包,安裝非常簡單)。

二、使用

預設安裝完成後,pyinstaller程式位於C:\Python27\Scripts目錄下,可以通過執行pyinstaller  python_script.py 產生可執行檔 ,產生的可執行檔位於執行所在目錄下的dist目錄下。其進階參數如下:

–distpath=path_to_executable 指定產生的可執行檔存放的目錄,預設存放在dist目錄下
–workpath=path_to_work_files 指定編譯中臨時檔案存放的目錄,預設存放在build目錄下
–clean 清理編譯時間的臨時檔案
-F, –onefile 產生單獨的exe檔案而不是檔案夾
-d, –debug 編譯為debug模式,有助於運行中擷取日誌資訊
–version-file=version_text_file 為exe檔案添加版本資訊,版本資訊可以通過運行pyi-grab_version加上要擷取版本資訊的exe檔案的路徑來產生,產生後的版本資訊檔可以按需求修改並作為--version-file的參數添加到要產生的exe檔案中去
-i <FILE.ico>, -i <FILE.exe,ID>, –icon=<FILE.ico>, –icon=<FILE.exe,ID>
為exe檔案添加表徵圖,可以指定表徵圖路徑或者從已存在的exe檔案中抽取特定的ID的表徵圖作為要產生的exe檔案的表徵圖
另外,還可以通過spec檔案來產生可執行檔 。具體命令如下:

pyinstaller specfile
或者

pyi-build specfile

註: spec檔案每次通過命令產生時都會存在,可以通過簡單的修改增加相應的功能,如加表徵圖,指定著作權檔案 。

這裡以之前寫的程式為例,可以通過如下命令產生一個可執行檔

C:\Users\thinkpad>cd /d C:\Users\thinkpad\Desktop\monitor
C:\Users\thinkpad\Desktop\monitor>C:\Python27\Scripts\pyinstaller.exe -F monitor.py

在monitor增加一個ico表徵圖檔案,操作方法如下:

C:\Users\thinkpad\Desktop\monitor>C:\Python27\Scripts\pyinstaller.exe -i alert.ico  -F  monitor.py

增加版本資訊檔方法如下:

C:\Users\thinkpad\Desktop\monitor>C:\Python27\Scripts\pyinstaller.exe -i alert.ico --version-file=file_version_info.txt -F  monitor.py
版本資訊的寫法看後面 。

此時的spec檔案內容如下:

# -*- mode: python -*-
block_cipher = None
a = Analysis(['monitor.py'],
             pathex=['C:\\Users\\thinkpad\\Desktop\\monitor'],
             binaries=None,
             datas=None,
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          name='monitor',
          debug=False,
          strip=False,
          upx=True,
          console=True , version-file=file_version_info.txt , icon='alert.ico')

三、upx壓縮

預設產生的python可執行檔有點大,可以通過upx程式進行壓縮,upx程式的首頁位於:https://upx.github.io/ ,目前windows版的最新upx為upx391w.zip ,下載後,將其中的upx.exe檔案放到C:\Python27目錄下(python可執行程式的安裝目錄),再次執行的時候,可以跟上--upx進行壓縮。預設存在時不指定也會進行壓縮,產生exe檔案時,在其日誌中可以發現如下內容:

1087 INFO: PyInstaller: 3.2
1087 INFO: Python: 2.7.12
1088 INFO: Platform: Windows-7-6.1.7600-SP0
1090 INFO: wrote C:\Users\thinkpad\Desktop\monitor\monitor.spec
1172 INFO: UPX is available.
1174 INFO: Extending PYTHONPATH with paths
['C:\\Users\\thinkpad\\Desktop\\monitor',
 'C:\\Users\\thinkpad\\Desktop\\monitor']

可以看到上面的upx是可用的。通過spec檔案也一樣,增加upx=True即可。

四、版本資訊檔的產生

可以通過grab_version.py先讀取一個已經存在的exe檔案,擷取其版本資訊,如下:

C:\Users\thinkpad\Desktop\monitor>C:\Python27\Lib\site-packages\PyInstaller\utils\cliutils\grab_version.py C:\Windows\System32\sc.exe
Version info written to: file_version_info.txt
將file_version_info.txt放到對應的目錄,並將內部的資訊修改後,可以按步驟2中的方法,產生帶有著作權資訊的可執行檔。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.