最近在学习用python开发深度学习工具,发现使用cx_Freeze对程序进行打包时,采用构建setup.py的时候出现很多不清楚的地方,现在详细说明setup.py文件的内容
os.environ['TCL_LIBRARY'] = "C:\\ProgramData\\Anaconda3\\tcl\\tcl8.6"
os.environ['TK_LIBRARY'] = "C:\\ProgramData\\Anaconda3\\tcl\\tk8.6"
include_files = [r"C:\ProgramData\Anaconda3\DLLs\tcl86t.dll",
r"C:\ProgramData\Anaconda3\DLLs\tk86t.dll",
]
1、上面的两个库文件都在python安装路径下可以找到,python运行时候依赖的库
options = {
'build_exe': {
'includes': 'atexit' ,
'include_files': include_files,
'packages':['cv2','numpy','xml.etree','tensorflow','libs.label_name_dict']
}
}
2、options是打包程序import的包,其中 'cv2','numpy','xml.etree','tensorflow'是安装的python包,'libs.label_name_dict'是当前项目里面的库,但是之前这么写,build的时候会提示找不到libs库,这是因为python没有将当前路径设置成系统路径,需要添加设置:sys.path.append(r'../')
executables = [
Executable('new_gui_temp.py', base=base),
]
3、executables = [Executable('new_gui_temp.py', base=base),]是打包的py文件,可以有多个
import sys
from cx_Freeze import setup, Executable
import os
sys.path.append(r'../')
os.environ['TCL_LIBRARY'] = "C:\\ProgramData\\Anaconda3\\tcl\\tcl8.6"
os.environ['TK_LIBRARY'] = "C:\\ProgramData\\Anaconda3\\tcl\\tk8.6"
include_files = [r"C:\ProgramData\Anaconda3\DLLs\tcl86t.dll",
r"C:\ProgramData\Anaconda3\DLLs\tk86t.dll",
]
base = None
if sys.platform == 'win32':
base = 'Win32GUI'
options = {
'build_exe': {
'includes': 'atexit' ,
'include_files': include_files,
'packages':['cv2','numpy','xml.etree','tensorflow','libs.label_name_dict']
}
}
executables = [
Executable('new_gui_temp.py', base=base),
]
setup(name='education',
version='0.1',
description='ImagePre software',
options=options,
executables=executables
)
最后执行编译 python setup.py 输出目录