將python文件打包爲windows linux下可執行的二進制文件

將python文件打包爲windows/linux下可執行的二進制文件

日期:2018年08月15日python

環境準備

  • 確保.py文件在當前python環境下可正常運行
  • python環境建議:python3.5(官方版)pyinstaller 3.3.1
  • anaconda3.6環境可能出問題(本人遇到過,github有人遇到相似問題,有人回覆表示疑似是anaconda所致)
  • pyinstaller當前版本在python3.5下表現最好。

打包成可執行文件

嘗試如下將py文件打包成(假設你的文件是script.py,以windows平臺爲例,–onefile 是一種options)linux

pyinstaller options script.py

生成的可執行文件位於dist目錄下,嘗試運行script.exe,若可正常運行,萬事大吉,結束。git

可能出現的問題

與scipy相關

許多錯誤都是由 scipy 引起github

若提示缺乏scipy._lib.messagestream解決方法以下:web

(1)首先生成中間文件.spec(上一步已經生成過了,能夠不生成)shell

pyi-makespec options script.py

(2)打開.spec文件windows

找到hiddenimports,將其後的空列表[]改爲 ['scipy._lib.messagestream'] ,保存。svg

(3)從 .spec 編譯文件3d

pyinstaller option script.spec

莫名其妙的缺乏dll

打包成一個exe ( option: - - onefile)可執行文件當然方便,有時候也會引起莫名其妙的dll缺失的問題,若此問題出現了,把 - -onefile這個選項刪掉就好,此時,可執行文件爲dist/script/script.exe 。code

參考文檔
pyinstaller簡潔教程
官方文檔