当开发完成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="图标绝对路径")])