py2exe 把python脚本转成windows下可执行文件

py2exe是一个可以把python脚本转成windows下的可执行程序的模块。

py2exe扩展自disutils模块,由查看C:\Python25\Lib\site-packages\py2exe\__init__.py文件可知:
[code]
"""
builds windows executables from Python scripts

New keywords for distutils' setup function specify what to build:

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'


py2exe options, to be specified in the options keyword to the setup function:

unbuffered - if true, use unbuffered binary stdout and stderr
optimize - string or int (0, 1, or 2)

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 where to build the final files
typelibs - list of gen_py generated typelibs to include (XXX more text needed)

Items in the console, windows, service or com_server list can also be
dictionaries to further customize the build process. The following
keys in the dictionary are recognized, most are optional:

modules (SERVICE, COM) - list of module names (required)
script (EXE) - list of python scripts (required)
dest_base - directory and basename for the executable
if a directory is contained, must be the same for all targets
create_exe (COM) - boolean, if false, don't build a server exe
create_dll (COM) - boolean, if false, don't build a server dll
bitmap_resources - list of 2-tuples (id, pathname)
icon_resources - list of 2-tuples (id, pathname)
other_resources - list of 3-tuples (resource_type, id, datastring)
"""
# py2exe/__init__.py

# 'import py2exe' imports this package, and two magic things happen:
#
# - the 'py2exe.build_exe' submodule is imported and installed as
# 'distutils.commands.py2exe' command
#
# - the default distutils Distribution class is replaced by the
# special one contained in this module.
#

__version__ = "0.6.8"

import distutils.dist, distutils.core, distutils.command, build_exe, sys

class Distribution(distutils.dist.Distribution):

def __init__(self, attrs):
self.ctypes_com_server = attrs.pop("ctypes_com_server", [])
self.com_server = attrs.pop("com_server", [])
self.service = attrs.pop("service", [])
self.windows = attrs.pop("windows", [])
self.console = attrs.pop("console", [])
self.isapi = attrs.pop("isapi", [])
self.zipfile = attrs.pop("zipfile", "library.zip")

distutils.dist.Distribution.__init__(self, attrs)

distutils.core.Distribution = Distribution

distutils.command.__all__.append('py2exe')

sys.modules['distutils.command.py2exe'] = build_exe
[/code]

简单的使用方式如下:
1. 创建一个python脚本hello.py
[code]
print 'hello'
[/code]

2. 在同目录下创建一个setup.py脚本
[code]
from distutils.core import setup
import py2exe

setup(console=['hello.py']
[/code]

3. 运行setup.py,通常参数是install,现在是py2exe
[code]
python setup.py py2exe
[/code]

4. 你要的exe文件出现了
[code]
cd dist
./hello.exe
[/code]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值