python利用py2exe生成exe文件

手册/FAQ (455) 2015-10-15 11:31:36

py2exe生成exe文件步骤:

1、安装py2exe,官网下载:http://www.py2exe.org/

2、制作发布程序setup.py,代码如下:

  •   # setup.py
  •   from distutils.core import setup
  •   import py2exe
  •  setup(console=["helloworld.py"])
  • cmd窗口运行”python setup.py py2exe”

常见问题汇总:

1、生成单个exe文件

py2exe生成单个exe文件,使用bundle_files选项值进行控制生成。

bundle_files:bundle dlls in the zipfile or the exe. Valid values for bundle_files
are: 3 = don’t bundle (default) 2 = bundle everything but the Python
interpreter 1 = bundle everything, including the Python interpreter——官方解释

bundle_files=3:不压缩任何文件;

bundle_files=2:压缩文件,除了python解释器以外;

bundle_files=1:压缩所有文件,包括Python解释器。

示例代码:setup(console=[{“script”:”helloworld.py”}], options={“py2exe”:{“bundle_files”:1}})

2、隐藏console(cmd命令行)窗口

隐藏console(cmd命令行)窗口,使用安装关键词windows进行控制生成。

console:list of scripts to convert into console exes

windows:list of scripts to convert into GUI exes

示例代码:setup(windows=[“helloworld.py”])

3、“ImportError: No module named sip”错误提示

“不存在名称为sip的模块”错误提示,使用includes选项值进行包含sip模块。

includes:list of module names to include

示例代码:setup(console=[{“script”:”helloworld.py”}], options={“py2exe”:{“includes”:[“sip”]}})

4、“找不到指定的模块。LoadLibrary(pythondll) failed”

“找不到指定的模块。LoadLibrary(pythondll) failed”错误提示,使用安装关键词zipfile进行控制生成。

zipfile:name of shared zipfile to generate; may specify a subdirectory; defaults to ‘library.zip’. If zipfile is set to None, the files will be bundled within the executable instead of ‘library.zip’.

zipfile安装关键词默认生成“library.zip”文件,exe程序缺少了这个打包文件就会提示上述错误。更多时候,我们希望的是程序单文件绿色化。

示例代码:setup(console=[{“script”:”zls.py”}],zipfile=None)

THE END