2016年7月5日 星期二

Python 轉成 Exe 執行檔-Py2Exe

Hello~ Everyone!!!

今天想跟各位聊聊寫 Python 有時會遇到的問題,如何將寫好的 Python Script ".py檔" 轉成 Windows 作業系統可執行的 ".exe檔" ???

雖然在 Windows 系統上也可以執行 ".py" 但是先決條件是系統需安裝好 Python 甚至 Script 呼叫的程式庫都必須一併安裝好,不然當執行 .py 程式時一定會出錯。這時解決之道就是將寫好的 .py 程式轉成 .exe 的執行檔,使用者只需 "Double Click" 就可以輕輕鬆鬆執行程式,完全不需理會系統有沒有安裝 Python 或呼叫的程式庫等等...這是多美麗的結局啊!!!

接下來就開始這一節內容Py2Exe 依慣例一樣先來一張這一節的圖解說明: 

Step 1: First have to install correct version for Python. Py2Exe officially only support Python 2.x doesn't support Python 3.x, for this case download the last version of Python 2.7.11 here or download from official website as below.
https://www.python.org/downloads/release/python-2711/


Step 2: Double click "python-2.7.11.msi" to install Python 2.7



Step 3: Keep as default "Install for all users" then click "Next >"


Step 4: Select destination directory for Python. Normally install at "C:\Python27\" then click "Next >"


Step 5: Add python.exe to Path, select "Will be installed on local hard drive"





Step 6: Click "Finish" to complete Python installation.


Step 7: Verify Python installation. Open "cmd" then type "python -V"


Step 8: Second, download Py2Exe from here or from official website as below.
https://sourceforge.net/projects/py2exe/files/py2exe/0.6.9/


Step 9: After download completed double click "py2exe-0.6.9.win32-py2.7.exe"



Step 10: Click "Next >


Step 11: Double check "Python Directory" and "Installation Directory" then click "Next >"


Step 12: Click "Next >"


Step 13: Click "Completed" for finish Py2Exe installation.


Step 14: This case have to install PyQt4 for the Python script file AddressBook_v10.py


Step 15: Third, download PyQt4 from here or from official websit as below.
https://www.riverbankcomputing.com/software/pyqt/download


Step 16: After download completed double click "PyQt4-4.11.4-"gpl-Py2.7-Qt4.8.7-x32.exe" 


Step 17: Click "Next >"


Step 18: License agreement, Click "I Agree"


Step 19: Keep check all components as default then click "Next >"


Step 20: Install location keep as default at "C:\Python27\" then click "Install"



Step 21: Click "Finish" for completed the installation.


Step 22: Now have to create a setup.py  for compiler Python script or download from here. (setup.py and AddressBook_v1.0.py all put in C:\Python27\)




Step 23: Open "cmd" and type "cd C:\Python27" then run "python setup.py py2exe" after Enter get the error message "MSVCP90.dll" has been missing.


Step 24: Download MSVCP90.dll from here then save under "C:\Python27\"


Step 25: Run "python setup.py py2exe" again.


Step 26: After compiled "AddressBook_v1.0.exe" has been created under "C:\Python27\dist\"



Step 27: Run "AddressBook_v1.0.exe" will get the error message in yellow.



Step 28: Modify setup.py become "setup_fix.py" or download from here.



Step 29: Run "python setup_fix.py py2exe"


Step 30: After finished compiler run "AddressBook_v1.0.exe" again.


Step 31: Can double click from"File Explorer" as well.


Step 32: Congratulation!!! It's work!!!


經過落落長的安裝 > 寫程式 > 執行編譯 > 有錯誤 > 改程式 > 再執行編譯 > 成功建立 .exe檔回想起來這過程是多漫長啊 @@,不過經由一步一步的說明,相信大家都可以成功完成的。

其實這些步驟說穿了,不外乎把握住下面幾個原則:

  • Python2.7、Py2Exe、PyQt4 都要下載對版本,目前測試結果只支援 x86 就是 32bit 的版本。
  • Py2Exe 目前只支援 Python 2.x 的版本,並不支援 Python 3.x 版本。
  • 執行 Py2Exe 編譯後,會在 C:\Python27\ 產生2個 Folder "build" 和 "dist"。
  • 一定要將 msvcp90.dll 放到 C:\Python27\ 的目錄底下。
  • 大部分的 setup.py 的寫法都用下面的方法:

from py2exe.build_exe import py2exe
from distutils.core import setup
setup( console=[{"script": "main.py"}] )
*大部分都會有錯誤出現!!!

官網有提供另一種寫法如下:
from distutils.core import setup
import py2exe
setup(windows=[{"script" : "main.py"}], options={"py2exe" : {"includes" : ["sip"]}})
*main.py for convert to main.exe

各位如果有認真看的話,應該不難發現 setup_fix.py 就是用以上這種方法寫的。在練習的過程如果真的還是遇到解不了的問題,歡迎留言提問,太陽哥再幫忙想辦法解決 :D
這一節我們就先介紹到這邊,我們下回見~ Bye Bye!!!

~ See you ~

參考出處:
https://www.python.org/
http://www.py2exe.org/index.cgi/FrontPage
https://www.riverbankcomputing.com/software/pyqt/download



沒有留言:

張貼留言