首页 > 代码库 > Python打包成exe

Python打包成exe

有个好网站http://www.lfd.uci.edu/~gohlke/pythonlibs/ , 这个网站可以下载许多非官方的python库的安装包。

1.首先去这个网站下载cx_freeze安装包后安装。
2.在C:\PythonXX\Scripts\ 下可以看到cxfreeze-quickstart.bat, XX为版本号
3.运行cxfreeze-quickstart.bat, 根据向导生成setup.py
     setup.py类似如下:

import
sys
from
cx_Freezeimportsetup, Executable

# Dependencies are automatically detected, but it might need
# fine tuning.
buildOptions = dict(packages = [], excludes = [], include_files={
"DcmStatistics.xsd", "dcm2xml.exe" , "readme.txt" , "DcmStatisticsResult.xslt" })

executable =
"DcmFileCompareTool.py"
base =
'Console'
targetName =
"dcmAnalyzer.exe"

executables = [
    Executable(executable, base=base, targetName=targetName)
]

setup(name=
'dcmanalyzer',
      version =
'1.0',
      description =
'convert dcm file to xml, parse xml and create statistics result, arrange dcm file to folders of different companies.',
      options = dict(build_exe = buildOptions),
      executables = executables)

4.executable指定py主文件, base指定生成Exe的类型(Console、Win32GUI)【在C:\PythonXX\Lib\site-packages\cx_Freeze\bases可以看到】, targetName指定生成exe的名字。

include_files指定打包一起的文件。

5.写个setup.bat, 执行编译
python setup.py build
pause
6.注意,有时打包出来的exe运行错误, 提示缺少inspect,解决办法:
     在py主文件的头部添加import inspect