天天看點

使用PyInstaller把Python腳本打包成exe

1、下載下傳位址:http://www.pyinstaller.org/ 。

2、解壓即用(V2.1)。注意環境變量,或者用pyinstaller.py的全路徑,或者到解壓目錄下使用。*^_^*

3、打包指令示例:

python pyinstaller.py --onefile yourprogram.py      

4、參數資訊參見手冊的Options 。

*** Updated 2015-12-11 ***

1、用pip安裝pyinstaller3。

pip3 install pyinstaller      

2、打包指令示例:

pyinstaller --onefile yourprogram.py      

另外兩個常用的參數:

(1)、添加圖示

-i walker.ico    //添加圖示      

(2)、壓縮可執行檔案(upx)

--upx-dir UPX_DIR    //指定壓縮程式(upx.exe)目錄,預設目前目錄      

FAQ:

Q1、ImportError: DLL load failed

A1、walker遇到的情況是引用了pyd檔案,而這個pyd檔案依賴其他dll庫,walker的解決方案是安裝VC2010運作時庫。

Q2、程式中用到多程序在打包時應注意些什麼?

A2、在main函數中添加multiprocessing.freeze_support()語句,參考這裡。

Q3、報如下幾種錯誤:

ImportError: No module named _mssql
ImportError: No module named decimal
ImportError: No module named uuid      

A3、很可能是代碼中用到了pymssql子產品,這時的解決步驟是:①、import decimal;②、import uuid;③、重新編譯;④、拷貝_mssql.pyd到釋出目錄。

Q4、打包成功,運作時報如下錯誤:

Traceback (most recent call last):
  File "main.py", line 9, in <module>
ModuleNotFoundError: No module named 'sub.py'
[4100] Failed to execute script main      

A4、walker 排查出的原因是 py 檔案編碼不一緻,main.py 是 utf8 編碼,sub.py 是 gbk 編碼,統一成 utf8 編碼後問題解決。

Q5、python 3.6,pyinstaller 3.3.1,pywinauto 0.6.4 打包問題可以參考:PyInstaller 3.3.1 does not work with Pywinauto lib import

A5、copy 如下:

Create folder comtypes/gen/ in the same folder of your test.py, 
and copy file _944DE083_8FB8_45CF_BCB7_C477ACB2F897_0_1_0.py and UIAutomationClient.py (under Lib/site-packages/comtypes/gen/) into this folder.
Then use pyinstaller --hidden-import comtypes.gen._944DE083_8FB8_45CF_BCB7_C477ACB2F897_0_1_0 --hidden-import comtypes.gen.UIAutomationClient test.py      

相關閱讀:

1、Python 打包可執行檔案

2、Linux下安裝pyinstaller用于将py檔案打包生成一個可執行檔案

3、pyinstaller無法在指令行運作,提示failed to create process.

繼續閱讀