py2exe使用方法 (含一些调试技巧,如压缩email 类)
一、简介
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示例如下:
from distutils.core import setup
import py2exesetup(console=["helloworld.py"])
如果显示错误提示的话 “ msvcp90.dll: no such file or directory”
请尝试下面的方法:
from distutils.core import setup
import py2exesetup(console=["helloworld.py"],options = { "py2exe": { "dll_excludes": ["MSVCP90.dll"] } }
)
然后按下面的方法运行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
Global options:--verbose (-v) run verbosely (default)--quiet (-q) run quietly (turns verbosity off)--dry-run (-n) don't actually do anything--help (-h) show detailed help messageOptions for 'py2exe' command:--optimize (-O) optimization level: -O1 for "python -O", -O2 for"python -OO", and -O0 to disable [default: -O0]--dist-dir (-d) directory to put final built distributions in (defaultis dist)--excludes (-e) comma-separated list of modules to exclude--dll-excludes comma-separated list of DLLs to exclude--ignores comma-separated list of modules to ignore if they arenot found--includes (-i) comma-separated list of modules to include--packages (-p) comma-separated list of packages to include--compressed (-c) create a compressed zipfile--xref (-x) create and show a module cross reference--bundle-files (-b) bundle dlls in the zipfile or the exe. Valid levelsare 1, 2, or 3 (default)--skip-archive do not place Python bytecode files in an archive, putthem directly in the file system--ascii (-a) do not automatically include encodings and codecs--custom-boot-script Python file that will be run when setting up theruntime environmentusage: setup_py2exe.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]or: setup_py2exe.py --help [cmd1 cmd2 ...]or: setup_py2exe.py --help-commandsor: setup_py2exe.py cmd --help
四、指定额外的文件
一些应用程序在运行时需要额外的文件,诸如配置文件、字体、位图。
如果在安装脚本中用data_files可选项指定了那些额外的文件,那么py2exe能将这些文件拷贝到dist子目录中。data_files应包含一个元组(target-dir, files)列表,其中的files是这些额外的文件的列表。
示例如下:
PythonCode: # mysetup.py
from distutils.core import setup
import glob
import py2exesetup(console=["helloworld.py"],data_files=[("bitmaps",["bm/large.gif", "bm/small.gif"]),("fonts",glob.glob("fonts\\*.fnt"))],
)
说明:data_files选项将创建一个子目录dist\bitmaps,其中包含两个.gif文件;一个子目录dist\fonts,其中包含了所有的.fnt文件。
五、Windows NT services
你可以通过传递一个service关键字参数给setup函数来建造Windows NT services
,这个service参数的值必须是一个Python模块名(包含一service类)的列表。
示例如下:
PythonCode: # mysetup.py
from distutils.core import setup
import py2exesetup(service=["MyService"])
所建造的可执行的service是可以通过在其后跟一定的命令行参数标记来自行安装和卸载的。你可以通过在这个可执行的service(exe)后跟一-help参数来得到更多的帮助。
六、COM servers
你可以通过传递一个com_server 关键字参数给setup函数来建造Windows NT services
,这个service参数的值必须是一个Python模块名(包含一个或多个COM server 类)的列表。
示例如下:
PythonCode: # mysetup.py
from distutils.core import setup
import py2exesetup(com_server=["win32com.server.interp"])
默认情况下,DLL和EXE servers被建造,你不需要它们的话你可以简单的删除它们。
一个标准的py2exe setup文件编写
-*- coding: cp936 -*-
from distutils.core import setupimport py2exeincludes = ["encodings", "encodings.*"]
#要包含的其它库文件options = {"py2exe":{"compressed": 1, #压缩"optimize": 2,"ascii": 1,"includes":includes,"bundle_files": 1 #所有文件打包成一个exe文件 }}
setup( options = options, zipfile=None, #不生成library.zip文件console=[{"script": "hello.py", "icon_resources": [(1, "hello.ico")] }]#源文件,程序图标)
新 版本已经可以打包为一个文件了,以前都是一堆dll,pyd的。具体的变化其实只有一个地方。就是options里增加bundle_files项,值为 1表示pyd和dll文件会被打包到exe文件中,且不能从文件系统中加载python模块;值为2表示pyd和dll文件会被打包到exe文件中,但是 可以从文件系统中加载python模块。另外setup中使用zipfile=None可以不生成library.zip。
例如原来 的:
from distutils.core import setup
import py2exe
includes = ["encodings", "encodings.*"]
options = {"py2exe": { "compressed": 1, "optimize": 2, "includes": includes, } }
setup( version = "0.1.0", description = "search panda", name = "search panda", options = options, windows=[{"script": "search.py", "icon_resources": [(1, "search.ico")] }], )
只需要改为:
from distutils.core import setup
import py2exe
includes = ["encodings", "encodings.*"]
options = {"py2exe": { "compressed": 1, "optimize": 2, "includes": includes, "bundle_files": 1 } }
setup( version = "0.1.0", description = "search panda", name = "search panda", options = options, zipfile=None, windows=[{"script": "search.py", "icon_resources": [(1, "search.ico")] }], )
比如,这里我打包以前的DelphiCode2HTML的
# -*- coding: gbk -*-from distutils.core import setup
import py2exeincludes = ["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:\我的程序\Python\CSDN Code Edit\Code2Html.py","icon_resources": [(1, "F:\书籍\我的图标\图标xp\Convert.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 |
Example:
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 py2exeincludes = ["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")] }], )
py2exe使用方法 (含一些调试技巧,如压缩email 类)相关推荐
- py2exe使用方法 (含一些调试技巧,如压缩email 类)(转)
一.简介 py2exe是一个将python脚本转换成windows上的可独立执行的可执行程序(*.exe)的工具,这样,你就可以不用装python而在windows系统上运行这个可执行程序. py2e ...
- c语言 vc 单步调试方法,VC6断点调试技巧
在第一篇中,我们为大家介绍了如何用VC6进行断点调试,可以实现程序的分步执行.今天我们将继续深入,进行更多断点调试的学习. 首先,写一个例子程序: #include int fun() { print ...
- py2exe用法举例与调试技巧
本文分享下python编程中py2exe的用法,以及py2exe的一些调试技巧,有需要的朋友参考下. 一.简介 py2exe是一个将python脚本转换成windows上的可独立执行的可执行程序(*. ...
- Visual Studio中11个强大的调试技巧和方法
调试是软件开发周期中很重要的一部分.它具有挑战性,同时也很让人疑惑和烦恼.总的来说,对于稍大一点的程序,调试是不可避免的.最近几年,调试工具的发展让很多调试任务变的越来越简单和省时. 这篇文章总结了可 ...
- vs c语言程序调试方法,VS2015中的常用调试技巧分享
原标题:VS2015中的常用调试技巧分享 为什么要学习调试? 调试(Debug)是作为一个程序员必须要学会的东西,学会调试可以极大的提高开发效率,排错时间,很多人不喜欢调试,但我认为这是一个很不可取的 ...
- NE问题分析方法 ---- Native栈还原和调试技巧
文章目录 简介 Native栈还原 调试技巧 简介 NE即Native Exception,我们主要指Android C/C++程序出现异常报错,因Camera HAL是由C/C++实现的,在相机系统 ...
- 东软医疗打印设置java,关于单据打印的调试方法常见问题与使用技巧
3.3.4.6.1什么是单据打印? 流程运转到特定的节点,需要把流程数据打印出来,这种根据流程数据生成特定格式文件的过程叫做单据打印. 单据打印首先需要根据用户的对打印的要求制作一个单据模板. 1, ...
- java中的匿名类方法覆盖_Java技巧:用匿名类来实现简化程序调试
Java技巧:用匿名类来实现简化程序调试 在Java中,匿名类(Anonymous inner classes)多用来处理事件(event handle).但其实,它们对于debug也很有帮助.本文将 ...
- 你可能不知道的 IDEA 高级调试技巧
作者:十光年 www.cnblogs.com/jun1019/p/9741224.html 一.条件断点 循环中经常用到这个技巧,比如:遍历1个大List的过程中,想让断点停在某个特定值. 参考上图, ...
最新文章
- [NOI2011]Noi嘉年华
- MATLAB读取excel文件中的数据
- 详细讲解Hyper-V虚拟机的网络配置
- java求职攻略_2020年求职攻略 《Java面试连成诀》教程免费分享
- POJ - 3678 Katu Puzzle(2-SAT)
- win7系统怎么查看主板信息
- HashMap的底层原理
- logback logback.xml 常用配置详解
- C程序设计语言现代方法14:预处理器
- concurrenthashmap_ConcurrentHashMap核心原理,这次彻底给整明白了
- How to get the android resolution
- C/C++笔记(01):容易出错的几个库函数
- 五子棋等级考试软件的开发(五)
- 10款网站后台管理系统模板_bootstrap网站后台模板_html后台模板下载(五)
- linux 防火墙服务器,Linux服务器上适用的防火墙分析
- 001 Joiner
- html5 手机uc浏览器 复制链接,清除UC手机浏览器强制在页面中加入的关键词链接...
- UIKit框架的介绍
- LLVM中的pass及其管理机制
- 禁止网页 切屏 切换标签
热门文章
- java代码鸟飞_180行原生js代码实现简易版飞行的小鸟游戏
- android 按钮顶级效果_人人都可写代码-Android零基础编程-相对布局11
- html css div显示隐藏,Html-Css-div透明层剧中
- C++/OpenCV:error C4996: ‘fopen‘: This function or variable may be unsafe.
- linux那些事之LRU(4)
- python3 字符串格式化_Python3-字符串格式化
- scss、sass 和 css 的区别
- ThinkPHP医疗门诊病人挂号管理在线付款系统源码
- Flat Tech html5 前端响应式模板
- 东方终焉组审核页可做引导页