py2exe用法举例与调试技巧

发布时间:2019-11-18编辑:脚本学堂
本文分享下python编程中py2exe的用法,以及py2exe的一些调试技巧,有需要的朋友参考下。
比如,这里打包以前的DelphiCode2HTML的:
 

复制代码 代码示例:
# -*- coding: gbk -*- 
 
from distutils.core import setup 
import py2exe 
 
includes = ["encodings", "encodings.*"] 
options = {"py2exe": 
                    {"compressed": 1, 
                     "optimize": 2, 
                     "ascii": 1, 
                     "includes":includes, 
                     "bundle_files": 1} 
           } 
setup( 
    options = options, 
    zipfile=None, 
    name = "HelloGuys.", 
    description = "this is a py2exe test",    
    windows=[{"script": "F:我的程序PythonCSDN Code EditCode2Html.py", 
              "icon_resources": [(1, "F:书籍我的图标图标xpConvert.ico")] 
              }] 
    ) 
 

 
列出options:
 

keyword
  description
data_files
  list of "data" files that you are going to need to run your executable such as .pngs, .jpgs
 
 
Py2exe extends Distutils setup keywords
 
In addition to the standard distutils setup keywords, the following py2exe keywords specify what and how to build.
keyword
description
console
list of scripts to convert into console exes
windows
list of scripts to convert into GUI exes
service
list of module names containing win32 service classes
com_server
list of module names containing com server classes
ctypes_com_server
list of module names containing com server classes
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'.
options
dictionary { "py2exe": { "opt1": val1, "opt2": val2, ... } }
 
 
The options dictionary of py2exe
 
The option keyword takes the following set of dictionary key: value pairs. The dictionary "key" names and the "value" types are listed in the table below.
 
key
value
unbuffered
if true, use unbuffered binary stdout and stderr
optimize
string or int of optimization level (0, 1, or 2) 0 = don’t optimize (generate .pyc) 1 = normal optimization (like python -O) 2 = extra optimization (like python -OO) See http://docs.python.org/distutils/apiref.html#module-distutils.util for more info.
includes
list of module names to include
packages
list of packages to include with subpackages
ignores
list of modules to ignore if they are not found
excludes
list of module names to exclude
dll_excludes
list of dlls to exclude
dist_dir
directory in which to build the final files
typelibs
list of gen_py generated typelibs to include
compressed
(boolean) create a compressed zipfile
xref
(boolean) create and show a module cross reference
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
skip_archive
(boolean) do not place Python bytecode files in an archive, put them directly in the file system
ascii
(boolean) do not automatically include encodings and codecs
custom-boot-script
Python file that will be run when setting up the runtime environment
 

 
例子:
 

复制代码 代码示例:
setup( 
        windows=['trypyglet.py'], 
        options={ 
                "py2exe":{ 
                        "unbuffered": True, 
                        "optimize": 2, 
                        "excludes": ["email"] 
                } 
        } 
)  
For more information enter the following at the python command line:
>>> from distutils.core import setup 
>>> help(setup) 
 

 
注意 windows 的用法,他可以代替 console, 如果你要集成 wxpython 的时候,一定会用的 !

更多请查看 http://www.py2exe.org/index.cgi/ListOfOptions
 
如果程序中含有email类,并且压缩时出现类似 “ImportError: No module named multipart ” 的错误,你需要如下的设置:
1. 尝试将Lib下的email包,复制到当前文件夹中
2. 把['emai'] 放入includes中
3. 把['email']放入packages中
4. 继续运行py2exe
 
例如:
 

复制代码 代码示例:
from distutils.core import setup  
import py2exe 
 
includes = ["encodings", "encodings.*",'email'] 
 
options = {"py2exe":  
            {   "compressed": 1,  
                "optimize": 2,  
                "includes": includes,  
                "bundle_files": 1, 
                "packages": ['email'], 
                "dll_excludes": ["MSVCP90.dll"] 
            }  
          }  
setup(     
    version = "0.1.0",  
    description = "3th",  
    name = "For My Lover",  
    options = options,  
    zipfile=None,  
    windows=[{"script": "love.py", "icon_resources": [(1, "roses.ico")] }],    
    )