基於cx_freeze編譯PyQt4程式(numpy & scipy),cx_freezepyqt4
當開發完成PyQt4程式後,需要提供給他人使用,這時最好的辦法是將Python程式編譯成exe檔案。
通常我採用cx_freeze完成這個工作,即編寫setup.py檔案,執行python setup.py build即可。
(1) 對於一般的PyQt4程式,setup.py內容如下:
1 import sys 2 3 from cx_Freeze import setup, Executable 4 5 base = None 6 if sys.platform == 'win32': 7 base = 'Win32GUI' 8 9 options = {10 'build_exe': {11 'includes': ['atexit'],12 'excludes': ['Tkinter',13 'collections.sys',14 'collections._weakref']15 }16 }17 18 setup(19 name="程式名",20 version="版本號碼",21 description="",22 options=options,23 executables=[Executable("主程式絕對路徑", base=base,24 icon="表徵圖絕對路徑")])
(2) 當使用numpy後,則setup.py修改為:
1 import sys 2 3 from cx_Freeze import setup, Executable 4 5 base = None 6 if sys.platform == 'win32': 7 base = 'Win32GUI' 8 9 options = {10 'build_exe': {11 'includes': ['atexit',12 'numpy'],13 'excludes': ['Tkinter',14 'collections.sys',15 'collections._weakref']16 }17 }18 19 setup(20 name="程式名",21 version="版本號碼",22 description="",23 options=options,24 executables=[Executable("主程式絕對路徑", base=base,25 icon="表徵圖絕對路徑")])
(3) 當使用scipy後,利用cx_freeze編譯時間會出現Import Error: No module named 'scipy'。
這時需要將cx_Freeze檔案夾下的hooks.py的第548行代碼"finder.IncludePackage("scipy.lib")"改為"finder.IncludePackage("scipy._lib")"。
相應的setup.py修改如下:
1 import sys 2 import numpy # 一定要有,否則會出現_ufuncs錯誤 3 4 from cx_Freeze import setup, Executable 5 6 base = None 7 if sys.platform == 'win32': 8 base = 'Win32GUI' 9 10 options = {11 'build_exe': {12 'packages': ['scipy'], # 重要13 'includes': ['atexit',14 'numpy',15 'scipy'],16 'excludes': ['Tkinter',17 'collections.sys',18 'collections._weakref']19 }20 }21 22 setup(23 name="程式名",24 version="版本號碼",25 description="",26 options=options,27 executables=[Executable("主程式絕對路徑", base=base,28 icon="表徵圖絕對路徑")])