打包工具: cx_Freeze
使用cx_Freeze完成python打包exe主要有两种方法:
第一种,直接运行cxfreeze.bat通过:
先进入cmd命令行,进入C:/Python25/Scripts目录,然后运行cxfreeze.bat -h 就可以看到它的使用说明了。我们可以用cx_freeze自己的sample做个试验。
进入到c:/python25/scripts/下,运行
C:\Python25\Scripts>cxfreeze C:\Users\caojj\workspace\hyAutoTools\src\mainFrm.p --install-dir=d:/123
pytqt4app.py就会被打包成exe,并且它所用到的库文件,也被一并考到了d:/123目录下。
第二种,方法,运行setup.py:
在cxfreeze附带的例子 C:/Python31/Lib/site-packages/cx_Freeze/samples/PyQt4 中,有一个setup.py文件,打开这个文件,我们发现其内容为:
通过运行这个脚本,就可以自动的完成打包工作:
比如,我们进入 C:/Python31/Lib/site-packages/cx_Freeze/samples/PyQt4目录下,
运行
setup.py build
之后,在此目录下会出现一个build/exe.win32-3.1目录,该目录下即可看到打包完成的exe文件了。 我们要想对自己的脚本打包,将这个setup.py 拷贝过去,再将其中
executables = [Executable("PyQt4app.py", base = base)])
的PyQt4app.py改为自己的脚本名即可。
注意: 打包过程中,如果包含模块
import sys from cx_Freeze import setup, Executable executables = [ Executable("advanced_1.py"), Executable("advanced_2.py") ] buildOptions = dict( compressed = True, includes = ["testfreeze_1", "testfreeze_2"], path = sys.path + ["modules"]) setup( name = "advanced_cx_Freeze_sample", version = "0.1", description = "Advanced sample cx_Freeze script", options = dict(build_exe = buildOptions), executables = executables)