python py2exe 创建exe可执行文件
python py2exe 创建exe可执行文件
2011年08月26日
下载安装py2exehttp://sourceforge.net/projects/py2exe/files/py2exe/
以上地址,可下载各个python相应版的py2exe版本.值得收藏!!
安装后的文件应该在你的python安装目录下的Lib\site-packages\py2exe
呵先来个简单的测试程序:
Hello.py文件内容如下 :
def main():
print "Hello, World!"
if __name__ == '__main__':
main()
然后为使用py2exe写一个脚本setup.py内容如下 :
from distutils.core import setup
import py2exe
setup(console=["Hello.py"])
运行setup.py,记得要传一个参数给它
进入你程序所存放目录如:
C:\Python26\hello\
输入:setup.py py2exe
*** finding dlls needed ***
*** create binaries ***
*** byte compile python files ***
byte-compiling C:\Python26\hello\build\bdist.win32\winexe\temp\bz2.py to bz2.pyc
byte-compiling C:\Python26\hello\build\bdist.win32\winexe\temp\select.py to sele
ct.pyc
byte-compiling C:\Python26\hello\build\bdist.win32\winexe\temp\unicodedata.py to
unicodedata.pyc
byte-compiling C:\Python26\lib\StringIO.py to StringIO.pyc
byte-compiling C:\Python26\lib\UserDict.py to UserDict.pyc
byte-compiling C:\Python26\lib\__future__.py to __future__.pyc
byte-compiling C:\Python26\lib\_abcoll.py to _abcoll.pyc
byte-compiling C:\Python26\lib\_strptime.py to _strptime.pyc
byte-compiling C:\Python26\lib\_threading_local.py to _threading_local.pyc
byte-compiling C:\Python26\lib\abc.py to abc.pyc
byte-compiling C:\Python26\lib\atexit.py to atexit.pyc
byte-compiling C:\Python26\lib\base64.py to base64.pyc
byte-compiling C:\Python26\lib\bdb.py to bdb.pyc
byte-compiling C:\Python26\lib\bisect.py to bisect.pyc
byte-compiling C:\Python26\lib\calendar.py to calendar.pyc
byte-compiling C:\Python26\lib\cmd.py to cmd.pyc
byte-compiling C:\Python26\lib\codecs.py to codecs.pyc
byte-compiling C:\Python26\lib\collections.py to collections.pyc
byte-compiling C:\Python26\lib\copy.py to copy.pyc
byte-compiling C:\Python26\lib\copy_reg.py to copy_reg.pyc
byte-compiling C:\Python26\lib\difflib.py to difflib.pyc
byte-compiling C:\Python26\lib\dis.py to dis.pyc
byte-compiling C:\Python26\lib\doctest.py to doctest.pyc
byte-compiling C:\Python26\lib\dummy_thread.py to dummy_thread.pyc
byte-compiling C:\Python26\lib\encodings\__init__.py to encodings\__init__.pyc
creating C:\Python26\hello\build\bdist.win32\winexe\collect-2.6\encodings
byte-compiling C:\Python26\lib\encodings\aliases.py to encodings\aliases.pyc
byte-compiling C:\Python26\lib\encodings\ascii.py to encodings\ascii.pyc
byte-compiling C:\Python26\lib\encodings\base64_codec.py to encodings\base64_cod
ec.pyc
byte-compiling C:\Python26\lib\encodings\big5.py to encodings\big5.pyc
byte-compiling C:\Python26\lib\encodings\big5hkscs.py to encodings\big5hkscs.pyc
byte-compiling C:\Python26\lib\encodings\bz2_codec.py to encodings\bz2_codec.pyc
byte-compiling C:\Python26\lib\encodings\charmap.py to encodings\charmap.pyc
byte-compiling C:\Python26\lib\encodings\cp037.py to encodings\cp037.pyc
byte-compiling C:\Python26\lib\encodings\cp1006.py to encodings\cp1006.pyc
byte-compiling C:\Python26\lib\encodings\cp1026.py to encodings\cp1026.pyc
byte-compiling C:\Python26\lib\encodings\cp1140.py to encodings\cp1140.pyc
byte-compiling C:\Python26\lib\encodings\cp1250.py to encodings\cp1250.pyc
byte-compiling C:\Python26\lib\encodings\cp1251.py to encodings\cp1251.pyc
byte-compiling C:\Python26\lib\encodings\cp1252.py to encodings\cp1252.pyc
byte-compiling C:\Python26\lib\encodings\cp1253.py to encodings\cp1253.pyc
byte-compiling C:\Python26\lib\encodings\cp1254.py to encodings\cp1254.pyc
byte-compiling C:\Python26\lib\encodings\cp1255.py to encodings\cp1255.pyc
byte-compiling C:\Python26\lib\encodings\cp1256.py to encodings\cp1256.pyc
byte-compiling C:\Python26\lib\encodings\cp1257.py to encodings\cp1257.pyc
byte-compiling C:\Python26\lib\encodings\cp1258.py to encodings\cp1258.pyc
byte-compiling C:\Python26\lib\encodings\cp424.py to encodings\cp424.pyc
byte-compiling C:\Python26\lib\encodings\cp437.py to encodings\cp437.pyc
byte-compiling C:\Python26\lib\encodings\cp500.py to encodings\cp500.pyc
byte-compiling C:\Python26\lib\encodings\cp737.py to encodings\cp737.pyc
byte-compiling C:\Python26\lib\encodings\cp775.py to encodings\cp775.pyc
byte-compiling C:\Python26\lib\encodings\cp850.py to encodings\cp850.pyc
byte-compiling C:\Python26\lib\encodings\cp852.py to encodings\cp852.pyc
byte-compiling C:\Python26\lib\encodings\cp855.py to encodings\cp855.pyc
byte-compiling C:\Python26\lib\encodings\cp856.py to encodings\cp856.pyc
byte-compiling C:\Python26\lib\encodings\cp857.py to encodings\cp857.pyc
byte-compiling C:\Python26\lib\encodings\cp860.py to encodings\cp860.pyc
byte-compiling C:\Python26\lib\encodings\cp861.py to encodings\cp861.pyc
byte-compiling C:\Python26\lib\encodings\cp862.py to encodings\cp862.pyc
byte-compiling C:\Python26\lib\encodings\cp863.py to encodings\cp863.pyc
byte-compiling C:\Python26\lib\encodings\cp864.py to encodings\cp864.pyc
byte-compiling C:\Python26\lib\encodings\cp865.py to encodings\cp865.pyc
byte-compiling C:\Python26\lib\encodings\cp866.py to encodings\cp866.pyc
byte-compiling C:\Python26\lib\encodings\cp869.py to encodings\cp869.pyc
byte-compiling C:\Python26\lib\encodings\cp874.py to encodings\cp874.pyc
byte-compiling C:\Python26\lib\encodings\cp875.py to encodings\cp875.pyc
byte-compiling C:\Python26\lib\encodings\cp932.py to encodings\cp932.pyc
byte-compiling C:\Python26\lib\encodings\cp949.py to encodings\cp949.pyc
byte-compiling C:\Python26\lib\encodings\cp950.py to encodings\cp950.pyc
byte-compiling C:\Python26\lib\encodings\euc_jis_2004.py to encodings\euc_jis_20
04.pyc
byte-compiling C:\Python26\lib\encodings\euc_jisx0213.py to encodings\euc_jisx02
13.pyc
byte-compiling C:\Python26\lib\encodings\euc_jp.py to encodings\euc_jp.pyc
byte-compiling C:\Python26\lib\encodings\euc_kr.py to encodings\euc_kr.pyc
byte-compiling C:\Python26\lib\encodings\gb18030.py to encodings\gb18030.pyc
byte-compiling C:\Python26\lib\encodings\gb2312.py to encodings\gb2312.pyc
byte-compiling C:\Python26\lib\encodings\gbk.py to encodings\gbk.pyc
byte-compiling C:\Python26\lib\encodings\hex_codec.py to encodings\hex_codec.pyc
byte-compiling C:\Python26\lib\encodings\hp_roman8.py to encodings\hp_roman8.pyc
byte-compiling C:\Python26\lib\encodings\hz.py to encodings\hz.pyc
byte-compiling C:\Python26\lib\encodings\idna.py to encodings\idna.pyc
byte-compiling C:\Python26\lib\encodings\iso2022_jp.py to encodings\iso2022_jp.p
yc
byte-compiling C:\Python26\lib\encodings\iso2022_jp_1.py to encodings\iso2022_jp
_1.pyc
byte-compiling C:\Python26\lib\encodings\iso2022_jp_2.py to encodings\iso2022_jp
_2.pyc
byte-compiling C:\Python26\lib\encodings\iso2022_jp_2004.py to encodings\iso2022
_jp_2004.pyc
byte-compiling C:\Python26\lib\encodings\iso2022_jp_3.py to encodings\iso2022_jp
_3.pyc
byte-compiling C:\Python26\lib\encodings\iso2022_jp_ext.py to encodings\iso2022_
jp_ext.pyc
byte-compiling C:\Python26\lib\encodings\iso2022_kr.py to encodings\iso2022_kr.p
yc
byte-compiling C:\Python26\lib\encodings\iso8859_1.py to encodings\iso8859_1.pyc
byte-compiling C:\Python26\lib\encodings\iso8859_10.py to encodings\iso8859_10.p
yc
byte-compiling C:\Python26\lib\encodings\iso8859_11.py to encodings\iso8859_11.p
yc
byte-compiling C:\Python26\lib\encodings\iso8859_13.py to encodings\iso8859_13.p
yc
byte-compiling C:\Python26\lib\encodings\iso8859_14.py to encodings\iso8859_14.p
yc
byte-compiling C:\Python26\lib\encodings\iso8859_15.py to encodings\iso8859_15.p
yc
byte-compiling C:\Python26\lib\encodings\iso8859_16.py to encodings\iso8859_16.p
yc
byte-compiling C:\Python26\lib\encodings\iso8859_2.py to encodings\iso8859_2.pyc
byte-compiling C:\Python26\lib\encodings\iso8859_3.py to encodings\iso8859_3.pyc
byte-compiling C:\Python26\lib\encodings\iso8859_4.py to encodings\iso8859_4.pyc
byte-compiling C:\Python26\lib\encodings\iso8859_5.py to encodings\iso8859_5.pyc
byte-compiling C:\Python26\lib\encodings\iso8859_6.py to encodings\iso8859_6.pyc
byte-compiling C:\Python26\lib\encodings\iso8859_7.py to encodings\iso8859_7.pyc
byte-compiling C:\Python26\lib\encodings\iso8859_8.py to encodings\iso8859_8.pyc
byte-compiling C:\Python26\lib\encodings\iso8859_9.py to encodings\iso8859_9.pyc
byte-compiling C:\Python26\lib\encodings\johab.py to encodings\johab.pyc
byte-compiling C:\Python26\lib\encodings\koi8_r.py to encodings\koi8_r.pyc
byte-compiling C:\Python26\lib\encodings\koi8_u.py to encodings\koi8_u.pyc
byte-compiling C:\Python26\lib\encodings\latin_1.py to encodings\latin_1.pyc
byte-compiling C:\Python26\lib\encodings\mac_arabic.py to encodings\mac_arabic.p
yc
byte-compiling C:\Python26\lib\encodings\mac_centeuro.py to encodings\mac_centeu
ro.pyc
byte-compiling C:\Python26\lib\encodings\mac_croatian.py to encodings\mac_croati
an.pyc
byte-compiling C:\Python26\lib\encodings\mac_cyrillic.py to encodings\mac_cyrill
ic.pyc
byte-compiling C:\Python26\lib\encodings\mac_farsi.py to encodings\mac_farsi.pyc
byte-compiling C:\Python26\lib\encodings\mac_greek.py to encodings\mac_greek.pyc
byte-compiling C:\Python26\lib\encodings\mac_iceland.py to encodings\mac_iceland
.pyc
byte-compiling C:\Python26\lib\encodings\mac_latin2.py to encodings\mac_latin2.p
yc
byte-compiling C:\Python26\lib\encodings\mac_roman.py to encodings\mac_roman.pyc
byte-compiling C:\Python26\lib\encodings\mac_romanian.py to encodings\mac_romani
an.pyc
byte-compiling C:\Python26\lib\encodings\mac_turkish.py to encodings\mac_turkish
.pyc
byte-compiling C:\Python26\lib\encodings\mbcs.py to encodings\mbcs.pyc
byte-compiling C:\Python26\lib\encodings\palmos.py to encodings\palmos.pyc
byte-compiling C:\Python26\lib\encodings\ptcp154.py to encodings\ptcp154.pyc
byte-compiling C:\Python26\lib\encodings\punycode.py to encodings\punycode.pyc
byte-compiling C:\Python26\lib\encodings\quopri_codec.py to encodings\quopri_cod
ec.pyc
byte-compiling C:\Python26\lib\encodings\raw_unicode_escape.py to encodings\raw_
unicode_escape.pyc
byte-compiling C:\Python26\lib\encodings\rot_13.py to encodings\rot_13.pyc
byte-compiling C:\Python26\lib\encodings\shift_jis.py to encodings\shift_jis.pyc
byte-compiling C:\Python26\lib\encodings\shift_jis_2004.py to encodings\shift_ji
s_2004.pyc
byte-compiling C:\Python26\lib\encodings\shift_jisx0213.py to encodings\shift_ji
sx0213.pyc
byte-compiling C:\Python26\lib\encodings\string_escape.py to encodings\string_es
cape.pyc
byte-compiling C:\Python26\lib\encodings\tis_620.py to encodings\tis_620.pyc
byte-compiling C:\Python26\lib\encodings\undefined.py to encodings\undefined.pyc
byte-compiling C:\Python26\lib\encodings\unicode_escape.py to encodings\unicode_
escape.pyc
byte-compiling C:\Python26\lib\encodings\unicode_internal.py to encodings\unicod
e_internal.pyc
byte-compiling C:\Python26\lib\encodings\utf_16.py to encodings\utf_16.pyc
byte-compiling C:\Python26\lib\encodings\utf_16_be.py to encodings\utf_16_be.pyc
byte-compiling C:\Python26\lib\encodings\utf_16_le.py to encodings\utf_16_le.pyc
byte-compiling C:\Python26\lib\encodings\utf_32.py to encodings\utf_32.pyc
byte-compiling C:\Python26\lib\encodings\utf_32_be.py to encodings\utf_32_be.pyc
byte-compiling C:\Python26\lib\encodings\utf_32_le.py to encodings\utf_32_le.pyc
byte-compiling C:\Python26\lib\encodings\utf_7.py to encodings\utf_7.pyc
byte-compiling C:\Python26\lib\encodings\utf_8.py to encodings\utf_8.pyc
byte-compiling C:\Python26\lib\encodings\utf_8_sig.py to encodings\utf_8_sig.pyc
byte-compiling C:\Python26\lib\encodings\uu_codec.py to encodings\uu_codec.pyc
byte-compiling C:\Python26\lib\encodings\zlib_codec.py to encodings\zlib_codec.p
yc
byte-compiling C:\Python26\lib\functools.py to functools.pyc
byte-compiling C:\Python26\lib\genericpath.py to genericpath.pyc
byte-compiling C:\Python26\lib\getopt.py to getopt.pyc
byte-compiling C:\Python26\lib\gettext.py to gettext.pyc
byte-compiling C:\Python26\lib\heapq.py to heapq.pyc
byte-compiling C:\Python26\lib\inspect.py to inspect.pyc
byte-compiling C:\Python26\lib\keyword.py to keyword.pyc
byte-compiling C:\Python26\lib\linecache.py to linecache.pyc
byte-compiling C:\Python26\lib\locale.py to locale.pyc
byte-compiling C:\Python26\lib\ntpath.py to ntpath.pyc
byte-compiling C:\Python26\lib\opcode.py to opcode.pyc
byte-compiling C:\Python26\lib\optparse.py to optparse.pyc
byte-compiling C:\Python26\lib\os.py to os.pyc
byte-compiling C:\Python26\lib\os2emxpath.py to os2emxpath.pyc
byte-compiling C:\Python26\lib\pdb.py to pdb.pyc
byte-compiling C:\Python26\lib\pickle.py to pickle.pyc
byte-compiling C:\Python26\lib\posixpath.py to posixpath.pyc
byte-compiling C:\Python26\lib\pprint.py to pprint.pyc
byte-compiling C:\Python26\lib\quopri.py to quopri.pyc
byte-compiling C:\Python26\lib\random.py to random.pyc
byte-compiling C:\Python26\lib\re.py to re.pyc
byte-compiling C:\Python26\lib\repr.py to repr.pyc
byte-compiling C:\Python26\lib\shlex.py to shlex.pyc
byte-compiling C:\Python26\lib\sre.py to sre.pyc
byte-compiling C:\Python26\lib\sre_compile.py to sre_compile.pyc
byte-compiling C:\Python26\lib\sre_constants.py to sre_constants.pyc
byte-compiling C:\Python26\lib\sre_parse.py to sre_parse.pyc
byte-compiling C:\Python26\lib\stat.py to stat.pyc
byte-compiling C:\Python26\lib\string.py to string.pyc
byte-compiling C:\Python26\lib\stringprep.py to stringprep.pyc
byte-compiling C:\Python26\lib\struct.py to struct.pyc
byte-compiling C:\Python26\lib\subprocess.py to subprocess.pyc
byte-compiling C:\Python26\lib\tempfile.py to tempfile.pyc
byte-compiling C:\Python26\lib\textwrap.py to textwrap.pyc
byte-compiling C:\Python26\lib\threading.py to threading.pyc
byte-compiling C:\Python26\lib\token.py to token.pyc
byte-compiling C:\Python26\lib\tokenize.py to tokenize.pyc
byte-compiling C:\Python26\lib\traceback.py to traceback.pyc
byte-compiling C:\Python26\lib\types.py to types.pyc
byte-compiling C:\Python26\lib\unittest.py to unittest.pyc
byte-compiling C:\Python26\lib\warnings.py to warnings.pyc
*** copy extensions ***
copying C:\Python26\DLLs\bz2.pyd -> C:\Python26\hello\dist
copying C:\Python26\DLLs\select.pyd -> C:\Python26\hello\dist
copying C:\Python26\DLLs\unicodedata.pyd -> C:\Python26\hello\dist
*** copy dlls ***
copying C:\WINDOWS\system32\python26.dll -> C:\Python26\hello\dist
setting sys.winver for 'C:\Python26\hello\dist\python26.dll' to 'py2exe'
copying C:\Python26\w9xpopen.exe -> C:\Python26\hello\dist
copying C:\Python26\lib\site-packages\py2exe\run.exe -> C:\Python26\hello\dist\h
ello.exe
*** binary dependencies ***
Your executable(s) also depend on these dlls which are not included,
you may or may not need to distribute them.
Make sure you have the license if you distribute any of them, and
make sure you don't distribute files belonging to the operating system.
WSOCK32.dll - C:\WINDOWS\system32\WSOCK32.dll
USER32.dll - C:\WINDOWS\system32\USER32.dll
ADVAPI32.dll - C:\WINDOWS\system32\ADVAPI32.dll
SHELL32.dll - C:\WINDOWS\system32\SHELL32.dll
KERNEL32.dll - C:\WINDOWS\system32\KERNEL32.dll
等一大串信息。
py2exe会在当前目录下生成两个目录 build和dist.
build里是一些py2exe运行时产生的中间文件,dist里有最终的可执行文件
现在可以运行hello.exe了
C:\Python26\hello\dist
进到这目录运行Hello.exe
即可看到输出内容.
不过记得如果要发布到别的机器上时,library.zip、 w9xpopen.exe、python23.dll这几个文件是必须要和hello.exe在一起的。