天天看點

python 使用 pyinstaller 打包 exe 檔案

python 使用 pyinstaller 打包 exe 檔案

在網上找pyinstaller資料時,發現大家提供的方法僅對簡單的程式有效,而略複雜的程式各種報錯,在這篇推文中我會闡述具有魯棒性的pyinstaller打包方法。官網:http://www.pyinstaller.org/

pyinstaller 常見參數

參數 解釋
-F, --onefile 生成單個exe檔案
-D, --onedir(預設) 生成一個exe檔案與依賴庫分開的目錄
–clean 清空上一次編譯産生的所有檔案
–specpath 生成spec檔案的路徑(預設目前路徑)
-n 生成spec檔案與exe檔案的名字
-p 指定額外import路徑
-c, --console(預設) 顯示指令行視窗
-w, --noconsole 不顯示指令行視窗
-i 修改icon(即修改程式圖示)

事先說明

  • 以下操作我是在虛拟環境進行
  • 打封包件為 real_time_video.py

準備工作

  • 進入項目路徑
F:
cd F:\code\pycharm\Emotion-recognition
           
  • 使用虛拟環境
venv\Scripts\activate
           
  • 下載下傳 pyinstaller
pip install pyinstaller
           

注意:如果後續出現了報錯:

FileNotFoundError: [Errno 2] No such file or directory: ‘…\astor\VERSION’

[8240] Failed to execute script test_exec

可以嘗試将actor降級至0.7.1(轉載:https://stackoverflow.com/questions/59985154/using-pyinstaller-to-convert-to-exe-filenotfounderror-c-users-8240-user)

pip uninstall actor
pip install actor==0.7.1
           

生成spec檔案

第一次打包該程式強烈建議不要關閉指令行視窗(下面這個語句是關閉了的,也可以在後面編輯spec檔案中修改)

pyi-makespec -w real_time_video.py
           

編輯spec檔案

執行上述語句後,spec檔案内容如下,接下來我們要在上面添加語句

# -*- mode: python ; coding: utf-8 -*-

block_cipher = None

a = Analysis(['real_time_video.py'],
             pathex=['F:\\code\\pycharm\\Emotion-recognition'],
             binaries=[],
             datas=[],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          [],
          exclude_binaries=True,
          name='real_time_video',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          console=True)
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=False,
               upx=True,
               upx_exclude=[],
               name='real_time_video')
           

pyinstaller會自動搜尋需要添加的檔案,但有的時候搜尋得并不全面,導緻瘋狂報錯,不如我們把檔案自己加進去。

  • Analysis

    以py檔案為輸入,分析py依賴子產品,生成相應資訊

    如果有多個py檔案存在聯系,可以在Analysis第一個括号内添加

Analysis(['real_time_video.py',
		  'xxx.py',
		  './otherFolder/xxx.py'],
		  ...)
           

如果有資源檔案,可以在datas添加

如果pyinstaller搜尋依賴子產品時漏了幾個,我們可以在hiddenimports添加

  • PYZ

    .pyz的壓縮包,包含程式運作需要的所有依賴

  • EXE

    根據上兩項生成

    如果需要修改exe名字,可以在name修改

根據自身需求是否顯示指令行視窗,在console修改

  • COLLECT

    生成其他部分的輸出檔案夾,COLLECT也可以沒有。如果想生成單個可執行檔案而不想要可執行目錄的話,可以把COLLECT的非重複内容添加到EXE,然後删除COLLECT。

# -*- mode: python ; coding: utf-8 -*-

block_cipher = None

a = Analysis(['real_time_video.py'],
             pathex=['F:\\code\\pycharm\\Emotion-recognition'],
             binaries=[],
             datas=[('./font','font'),('./models', 'models'),('./haarcascade_files','haarcascade_files')],
             hiddenimports=['imutils','keras.models'],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          [],
          exclude_binaries=True,
          name='Emotion-recognition',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          console=True)
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=False,
               upx=True,
               upx_exclude=[],
               name='Emotion-recognition')
           

編輯完後儲存,切換到指令行工具

執行spec檔案

這個時候使用pyinstaller執行spec檔案就開始打包了

注意:千萬不要寫成pyinstaller real_time_video.py,運作py檔案将自動生成spec檔案,會将原來我們修改的spec檔案覆寫掉

pyinstaller real_time_video.spec
           

當然也可以使用自定義參數

pyinstaller -D real_time_video.spec
           

之後耐心等待完成即可,這期間可能會出現問題,但是這種問題會因為程式的不同而不同,或者不會出現問題,這裡不進行贅述。在上述生成spec檔案時,在目前目錄會生成build檔案夾和dist檔案夾,生成的可執行檔案會儲存在dist檔案夾内。

驗證是否打包成功

仍是在指令行工具中,進入dist檔案夾内exe檔案路徑,執行所生成的exe檔案。

cd dist\Emotion-recognition
Emotion-recognition.exe
           

如果打包時設定指令行視窗是顯示的,此時如果程式出現錯誤,将在指令行視窗内顯示錯誤報告(我的程式是沒有錯誤的,但是我仍删掉了一個資源檔案模拟錯誤)

python 使用 pyinstaller 打包 exe 檔案

我們根據錯誤報告去修改就可以了,需要注意的是,缺什麼檔案直接丢到exe檔案的路徑就可以了,但這個過程在spec檔案的編寫中就已經告訴pyinstaller需要添加什麼檔案,不會出現這種錯誤,但是有一種情況:如果生成的是單個可執行檔案(exe檔案),那麼資源檔案是需要添加到exe檔案路徑下的,原因可能是因為,如果在你寫的程式中,調用資源檔案的方式使用的是路徑的方式,那麼程式便會按路徑的方式搜尋。如下:

detection_model_path = 'haarcascade_files/haarcascade_frontalface_default.xml'
emotion_model_path = 'models/_mini_XCEPTION.102-0.66.hdf5'
           

直接把haarcascade_files檔案夾和models檔案夾扔到exe檔案路徑下即可。

确認無誤後,把指令行視窗設定為不顯示,重新生成一次exe檔案就可以了(需要使用–clean指令或者直接删除build和dist檔案夾)

至此,pyinstaller 打包exe檔案說明結束。

感謝:

https://blog.csdn.net/weixin_42052836/article/details/82315118

https://blog.csdn.net/weixin_39000819/article/details/80942423