设为首页 加入收藏

TOP

基于cx_freeze编译PyQt4程序(numpy & scipy)
2017-09-30 16:55:31 】 浏览:3592
Tags:基于 cx_freeze 编译 PyQt4 程序 numpy scipy

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

 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇基于python3的手机号生成脚本 下一篇Python开发环境配置

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目