cxfreeze打包过程中最好通过setup.py来进行打包,打包的python程序需由模块+单个文件方式进行组织,否则会出现导入错误(ImportError),打包的setup.py配置如下:
# coding=utf-8
from cx_Freeze import setup, Executable
# python setup.py build
includefiles = ['README.md', 'event.ini'];;配置文件不用打包,相对路径
includes = []
excludes = []
packages = ['openpyxl', 'aiohttp', 'xlrd']
setup(name = "simple" ,
version = "0.1" ,
description = "A event export" ,
author = "wyt",
author_email = "test@qq.com",
options = {'build_exe': {'includes' :includes, 'excludes': excludes, 'packages' : packages, 'include_files': includefiles}},
executables = [Executable("main.py")]
)
通过python setup.py build来生成打包文件夹build。注意实现如下:
1. 打包后依赖的模块在lib文件夹下,如模块中涉及相对路径的引用,运行起来肯定会报错,建议不要使用相对路径访问文件。
2. 所有配置项放在python标准配置文件ini中,在打包时单独跳过该文件。
3. 依赖库均以pyc文件格式存放在库文件夹中,如出现找不到库的错误,则在对应的库中查找该文件是否存在。如以下错误:
File "C:\Python36\lib\asyncio\coroutines.py", line 13, in <module>
from . import events
ImportError: cannot import name 'events'
我们就去检查lib下asyncio中是否存在events.pyc文件,如果不存在就去系统库中拷贝过来。
4. cxfreeze打包会将该目录下所有python文件导入的包复制到lib文件夹下,我们可根据系统运行情况选择性的删除一些依赖库。