http://justcoding.iteye.com/blog/900993

一、简介

py2exe是一个将python脚本转换成windows上的可独立执行的可执行程序(*.exe)的工具,这样,你就可以不用装python而在windows系统上运行这个可执行程序。

py2exe已经被用于创建wxPython,Tkinter,Pmw,PyGTK,pygame,win32com client和server,和其它的独立程序。py2exe是发布在开源许可证下的。

二、安装py2exe

http://prdownloads.sourceforge.net/py2exe 下载并运行与你所安装的Python对应的py2exe版本的installer,这将安装py2exe和相应的例子;这些例子被安装在lib\site-packages\py2exe\samples目录下。

三、py2exe的用法

如果你有一个名为helloworld.py的python脚本,你想把它转换为运行在windows上的可执行程 序,并运行在没有安装python的 windows系统上,那么首先你应写一个用于发布程序的设置脚本例如mysetup.py,在其中的setup函数前插入语句 import py2exe 。

mysetup.py示例如下:

  1. from distutils.core import setup
  2. import py2exe
  3. setup(console=["helloworld.py"])

如果显示错误提示的话 “ msvcp90.dll: no such file or directory”

请尝试下面的方法:

  1. from distutils.core import setup
  2. import py2exe
  3. setup(
  4. console=["helloworld.py"],
  5. options = { "py2exe": { "dll_excludes": ["MSVCP90.dll"] } }
  6. )

然后按下面的方法运行mysetup.py: (dos:  cmd => cd desktop => mysetup.py py2exe)
python mysetup.py py2exe

上面的命令执行后将产生一个名为dist的子目录,其中包含了helloworld.exe,python24.dll,library.zip这些文件。
如果你的helloworld.py脚本中用了已编译的C扩展模块,那么这些模块也会被拷贝在个子目录中,同样,所有的dll文件在运行时都是需要的,除了系统的dll文件。

dist子目录中的文件包含了你的程序所必须的东西,你应将这个子目录中的所有内容一起发布。

默认情况下,py2exe在目录dist下创建以下这些必须的文件:
1、一个或多个exe文件。
2、python##.dll。 
3、几个.pyd文件,它们是已编译的扩展名,它们是exe文件所需要的;加上其它的.dll文件,这些.dll是.pyd所需要的。
4、一个library.zip文件,它包含了已编译的纯的python模块如.pyc或.pyo

上面的mysetup.py创建了一个控制台的helloword.exe程序,如果你要创建一个图形用户界的程序,那么你只需要将mysetup.py中的console=["helloworld.py"]替换为windows=["myscript.py"]既可。

py2exe一次能够创建多个exe文件,你需要将这些脚本文件的列表传递给console或windows的关键字参数。如果你有几个相关联的脚本,那么这是很有用的。

运行下面个命令,将显示py2exe命令的所有命令行标记。
python mysetup.py py2exe --help

  1. Global options:
  2. --verbose (-v)  run verbosely (default)
  3. --quiet (-q)    run quietly (turns verbosity off)
  4. --dry-run (-n)  don't actually do anything
  5. --help (-h)     show detailed help message
  6. Options for 'py2exe' command:
  7. --optimize (-O)       optimization level: -O1 for "python -O", -O2 for
  8. "python -OO", and -O0 to disable [default: -O0]
  9. --dist-dir (-d)       directory to put final built distributions in (default
  10. is dist)
  11. --excludes (-e)       comma-separated list of modules to exclude
  12. --dll-excludes        comma-separated list of DLLs to exclude
  13. --ignores             comma-separated list of modules to ignore if they are
  14. not found
  15. --includes (-i)       comma-separated list of modules to include
  16. --packages (-p)       comma-separated list of packages to include
  17. --compressed (-c)     create a compressed zipfile
  18. --xref (-x)           create and show a module cross reference
  19. --bundle-files (-b)   bundle dlls in the zipfile or the exe. Valid levels
  20. are 1, 2, or 3 (default)
  21. --skip-archive        do not place Python bytecode files in an archive, put
  22. them directly in the file system
  23. --ascii (-a)          do not automatically include encodings and codecs
  24. --custom-boot-script  Python file that will be run when setting up the
  25. runtime environment
  26. usage: setup_py2exe.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
  27. or: setup_py2exe.py --help [cmd1 cmd2 ...]
  28. or: setup_py2exe.py --help-commands
  29. or: setup_py2exe.py cmd --help

 

四、指定额外的文件

一些应用程序在运行时需要额外的文件,诸如配置文件、字体、位图。
如果在安装脚本中用data_files可选项指定了那些额外的文件,那么py2exe能将这些文件拷贝到dist子目录中。data_files应包含一个元组(target-dir, files)列表,其中的files是这些额外的文件的列表。

示例如下:

PythonCode:  # mysetup.py

  1. from distutils.core import setup
  2. import glob
  3. import py2exe
  4. setup(console=["helloworld.py"],
  5. data_files=[("bitmaps",
  6. ["bm/large.gif", "bm/small.gif"]),
  7. ("fonts",
  8. glob.glob("fonts\\*.fnt"))],
  9. )

说明:data_files选项将创建一个子目录dist\bitmaps,其中包含两个.gif文件;一个子目录dist\fonts,其中包含了所有的.fnt文件。

五、Windows NT services

你可以通过传递一个service关键字参数给setup函数来建造Windows NT services
,这个service参数的值必须是一个Python模块名(包含一service类)的列表。

示例如下:

PythonCode:  # mysetup.py

  1. from distutils.core import setup
  2. import py2exe
  3. setup(service=["MyService"])

所建造的可执行的service是可以通过在其后跟一定的命令行参数标记来自行安装和卸载的。你可以通过在这个可执行的service(exe)后跟一-help参数来得到更多的帮助。

六、COM servers

你可以通过传递一个com_server 关键字参数给setup函数来建造Windows NT services
,这个service参数的值必须是一个Python模块名(包含一个或多个COM server 类)的列表。

示例如下:

PythonCode:  # mysetup.py

  1. from distutils.core import setup
  2. import py2exe
  3. setup(com_server=["win32com.server.interp"])

默认情况下,DLL和EXE servers被建造,你不需要它们的话你可以简单的删除它们。

一个标准的py2exe setup文件编写

  1. -*- coding: cp936 -*-
  2. from distutils.core import setup
  3. import py2exe
  4. includes = ["encodings", "encodings.*"]
  5. #要包含的其它库文件
  6. options = {"py2exe":
  7. {"compressed": 1, #压缩
  8. "optimize": 2,
  9. "ascii": 1,
  10. "includes":includes,
  11. "bundle_files": 1 #所有文件打包成一个exe文件 }
  12. }
  13. setup(
  14. options = options,
  15. zipfile=None,   #不生成library.zip文件
  16. console=[{"script": "hello.py", "icon_resources": [(1, "hello.ico")] }]#源文件,程序图标
  17. )

新 版本已经可以打包为一个文件了,以前都是一堆dll,pyd的。具体的变化其实只有一个地方。就是options里增加bundle_files项,值为 1表示pyd和dll文件会被打包到exe文件中,且不能从文件系统中加载python模块;值为2表示pyd和dll文件会被打包到exe文件中,但是 可以从文件系统中加载python模块。另外setup中使用zipfile=None可以不生成library.zip。

例如原来 的:

  1. from distutils.core import setup
  2. import py2exe
  3. includes = ["encodings", "encodings.*"]
  4. options = {"py2exe":
  5. {   "compressed": 1,
  6. "optimize": 2,
  7. "includes": includes,
  8. }
  9. }
  10. setup(
  11. version = "0.1.0",
  12. description = "search panda",
  13. name = "search panda",
  14. options = options,
  15. windows=[{"script": "search.py", "icon_resources": [(1, "search.ico")] }],
  16. )

只需要改为:

  1. from distutils.core import setup
  2. import py2exe
  3. includes = ["encodings", "encodings.*"]
  4. options = {"py2exe":
  5. {   "compressed": 1,
  6. "optimize": 2,
  7. "includes": includes,
  8. "bundle_files": 1
  9. }
  10. }
  11. setup(
  12. version = "0.1.0",
  13. description = "search panda",
  14. name = "search panda",
  15. options = options,
  16. zipfile=None,
  17. windows=[{"script": "search.py", "icon_resources": [(1, "search.ico")] }],
  18. )

比如,这里我打包以前的DelphiCode2HTML的

  1. # -*- coding: gbk -*-
  2. from distutils.core import setup
  3. import py2exe
  4. includes = ["encodings", "encodings.*"]
  5. options = {"py2exe":
  6. {"compressed": 1,
  7. "optimize": 2,
  8. "ascii": 1,
  9. "includes":includes,
  10. "bundle_files": 1}
  11. }
  12. setup(
  13. options = options,
  14. zipfile=None,
  15. name = "HelloGuys.",
  16. description = "this is a py2exe test",
  17. windows=[{"script": "F:\我的程序\Python\CSDN Code Edit\Code2Html.py",
  18. "icon_resources": [(1, "F:\书籍\我的图标\图标xp\Convert.ico")]
  19. }]
  20. )

下面列出他的一些 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

Example:

  1. setup(
  2. windows=['trypyglet.py'],
  3. options={
  4. "py2exe":{
  5. "unbuffered": True,
  6. "optimize": 2,
  7. "excludes": ["email"]
  8. }
  9. }
  10. )

For more information enter the following at the python command line:

  1. >>> from distutils.core import setup
  2. >>> 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

如:

    1. from distutils.core import setup
    2. import py2exe
    3. includes = ["encodings", "encodings.*",'email']
    4. options = {"py2exe":
    5. {   "compressed": 1,
    6. "optimize": 2,
    7. "includes": includes,
    8. "bundle_files": 1,
    9. "packages": ['email'],
    10. "dll_excludes": ["MSVCP90.dll"]
    11. }
    12. }
    13. setup(
    14. version = "0.1.0",
    15. description = "3th",
    16. name = "For My Lover",
    17. options = options,
    18. zipfile=None,
    19. windows=[{"script": "love.py", "icon_resources": [(1, "roses.ico")] }],
    20. )

py2exe使用方法 (含一些调试技巧,如压缩email 类)的更多相关文章

  1. py2exe使用方法 (含一些调试技巧,如压缩email 类)(转)

    一.简介 py2exe是一个将python脚本转换成windows上的可独立执行的可执行程序(*.exe)的工具,这样,你就可以不用装python而在windows系统上运行这个可执行程序. py2e ...

  2. vs2010的11个调试技巧和方法

    调试是软件开发周期中很重要的一部分.它具有挑战性,同时也很让人疑惑和烦恼.总的来说,对于稍大一点的程序,调试是不可避免的.最近几年,调试工具的发展让很多调试任务变的越来越简单和省时. 这篇文章总结了可 ...

  3. 【工具】VS2010常用调试技巧(1)

    调试是一个程序员最基本的技能,其重要性不言自明.不会调试的程序员就意味着他即使会一门语言,却不能编制出好的软件.本文就本人在开发过程中常用的调试技巧作下简单呢介绍,希望对大家有所帮助,能力超群者请绕道 ...

  4. Visual Studio高级调试技巧

    1. 设置软件断点,运行到目标位置启动调试器 方法①:使用汇编指令(注:x64 c++不支持嵌入汇编) _asm 方法②:编译器提供的方法 __debugbreak(); 方法③:使用windows ...

  5. 【转】你所不知道的Android Studio调试技巧

    这篇写Android studio debug技巧个人觉得写得不错,转自:http://www.jianshu.com/p/011eb88f4e0d# Android Studio目前已经成为开发An ...

  6. VS调试技巧,提高调试效率(转):

    如果你还没有使用过这些技巧,希望这篇博文能帮你发现它们. 它们学起来很容易,能帮你节省很多时间. 运行到光标(Ctrl+ F10) 我经常看见人们是这样来调试应用程序的: 他们在应用程序需要调试的代码 ...

  7. iOS各种调试技巧豪华套餐

    转载自http://www.cnblogs.com/daiweilai/p/4421340.html 目录 前言 逼优鸡 知己知彼 百战不殆 抽刀断Bug 普通操作 全局断点(Global Break ...

  8. 你所不知道的Android Studio调试技巧

    转载:http://www.jianshu.com/p/011eb88f4e0d Android Studio目前已经成为开发Android的主要工具,用熟了可谓相当顺手.作为开发者,调试并发现bug ...

  9. Visual Studio原生开发的20条调试技巧(下)

    我的上篇文章<Vistual Studio原生开发的10个调试技巧>引发了很多人的兴趣,所以我决定跟大家分享更多的调试技巧.接下来你又能看到一些对于原生应用程序的很有帮助的调试技巧(接着上 ...

随机推荐

  1. Truncate a string-freecodecamp算法题目

    Truncate a string(截断字符串) 要求 如果字符串的长度比指定的参数num长,则把多余的部分用...来表示. 插入到字符串尾部的三个点号也会计入字符串的长度. 如果指定的参数num小于 ...

  2. Linux - which xxx - 查找执行的命令所在的路径

    Linux 下,我们常使用 cd ,grep,vi 等命令,有时候我们要查到这些命令所在的位置,如何做呢? Linux 下有2个命令可完成该功能:which ,whereis which 用来查看当 ...

  3. 素材网站——mokuge

  4. 【Python学习之四】递归与尾递归

    看完廖雪峰老师的教程,感觉尾递归函数是一个相对难点.于是复习一下,思考了一下,发表一些见解,记录一下. 1.递归函数 在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数 ...

  5. Python——format汇总

    一.str.format 按照指定格式格式化字符串,然后返回格式化的字符串,源字符串不变. 以下是Python2.7环境. 1.1.按照位置替换 参考下面例子: >>> s = '{ ...

  6. MySQL使用yum安装

    1.下载mysql的repo源 $ wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm 2.安装mysql-comm ...

  7. Linux学习-以最新核心版本编译 CentOS 7.x 的核心

    为了某些缘故需要最新的 4.x.y 的核心版本来实作某些特定的功能时,那该 如何是好?没办法,只好使用最新的核心版本来编译你可以依照上面的程序来一个一个处理, 没有问题~不过,你也可以根据 ELRep ...

  8. Centos7 开机显示 ERST: Failed to get Error Log Address Range” 导致无法开机解决方法

    开机显示 ERST: Failed to get Error Log Address Range”   导致无法开机,也无法重新安装系统,解决方法:开机进入BIOS , 关闭ACPI选项即可正常开机

  9. kafka消息的可靠性

    本文来自网易云社区 作者:田宏增 Kafka的高可靠性的保障来源于其健壮的副本(replication)策略.通过调节其副本相关参数,可以使得Kafka在性能和可靠性之间运转的游刃有余.Kafka从0 ...

  10. ios开发讲解之anchorPoint和position详解

    引言 相信初接触到CALayer的人都会遇到以下几个问题:  为什么修改anchorPoint会移动layer的位置? CALayer的position点是哪一点呢? anchorPoint与posi ...