您的位置:首页 > 编程语言 > Python开发

如何将python脚本生成exe

2017-06-21 18:15 267 查看
一、简介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 。

在同一目录存放两个python文件,一个是hello.py,一个是setup.py,前者是供测试的python文件,后者是打包用的程序:

print "Hello World!"
from distutils.core import setup
import py2exe
setup(console=["hello.py"])


命令行方式运行setup.py,如下:
python setup.py py2exe

命令行显示:

Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。

C:\Users\sean>e:

E:\>cd python

E:\python>python setup.py py2exe
running py2exe
creating E:\python\build
creating E:\python\build\bdist.win32
creating E:\python\build\bdist.win32\winexe
creating E:\python\build\bdist.win32\winexe\collect-2.7
creating E:\python\build\bdist.win32\winexe\bundle-2.7
creating E:\python\build\bdist.win32\winexe\temp
creating E:\python\dist
*** searching for required modules ***
*** parsing results ***
creating python loader for extension 'select' (C:\Python27\DLLs\select.pyd -> select.pyd)
creating python loader for extension 'unicodedata' (C:\Python27\DLLs\unicodedata.pyd -> unicodedata.pyd)
creating python loader for extension 'bz2' (C:\Python27\DLLs\bz2.pyd -> bz2.pyd)
*** finding dlls needed ***
*** create binaries ***
*** byte compile python files ***
byte-compiling C:\Python27\lib\StringIO.py to StringIO.pyc
byte-compiling C:\Python27\lib\UserDict.py to UserDict.pyc
byte-compiling C:\Python27\lib\__future__.py to __future__.pyc
byte-compiling C:\Python27\lib\_abcoll.py to _abcoll.pyc
byte-compiling C:\Python27\lib\_strptime.py to _strptime.pyc
byte-compiling C:\Python27\lib\_threading_local.py to _threading_local.pyc
byte-compiling C:\Python27\lib\_weakrefset.py to _weakrefset.pyc
byte-compiling C:\Python27\lib\abc.py to abc.pyc
byte-compiling C:\Python27\lib\atexit.py to atexit.pyc
byte-compiling C:\Python27\lib\base64.py to base64.pyc
byte-compiling C:\Python27\lib\bdb.py to bdb.pyc
byte-compiling C:\Python27\lib\bisect.py to bisect.pyc
byte-compiling C:\Python27\lib\calendar.py to calendar.pyc
byte-compiling C:\Python27\lib\cmd.py to cmd.pyc
byte-compiling C:\Python27\lib\codecs.py to codecs.pyc
byte-compiling C:\Python27\lib\collections.py to collections.pyc
byte-compiling C:\Python27\lib\copy.py to copy.pyc
byte-compiling C:\Python27\lib\copy_reg.py to copy_reg.pyc
byte-compiling C:\Python27\lib\difflib.py to difflib.pyc
byte-compiling C:\Python27\lib\dis.py to dis.pyc
byte-compiling C:\Python27\lib\doctest.py to doctest.pyc
byte-compiling C:\Python27\lib\dummy_thread.py to dummy_thread.pyc
byte-compiling C:\Python27\lib\encodings\__init__.py to encodings\__init__.pyc
creating E:\python\build\bdist.win32\winexe\collect-2.7\encodings
byte-compiling C:\Python27\lib\encodings\aliases.py to encodings\aliases.pyc
byte-compiling C:\Python27\lib\encodings\ascii.py to encodings\ascii.pyc
byte-compiling C:\Python27\lib\encodings\base64_codec.py to encodings\base64_codec.pyc
byte-compiling C:\Python27\lib\encodings\big5.py to encodings\big5.pyc
byte-compiling C:\Python27\lib\encodings\big5hkscs.py to encodings\big5hkscs.pyc
byte-compiling C:\Python27\lib\encodings\bz2_codec.py to encodings\bz2_codec.pyc
byte-compiling C:\Python27\lib\encodings\charmap.py to encodings\charmap.pyc
byte-compiling C:\Python27\lib\encodings\cp037.py to encodings\cp037.pyc
byte-compiling C:\Python27\lib\encodings\cp1006.py to encodings\cp1006.pyc
byte-compiling C:\Python27\lib\encodings\cp1026.py to encodings\cp1026.pyc
byte-compiling C:\Python27\lib\encodings\cp1140.py to encodings\cp1140.pyc
byte-compiling C:\Python27\lib\encodings\cp1250.py to encodings\cp1250.pyc
byte-compiling C:\Python27\lib\encodings\cp1251.py to encodings\cp1251.pyc
byte-compiling C:\Python27\lib\encodings\cp1252.py to encodings\cp1252.pyc
byte-compiling C:\Python27\lib\encodings\cp1253.py to encodings\cp1253.pyc
byte-compiling C:\Python27\lib\encodings\cp1254.py to encodings\cp1254.pyc
byte-compiling C:\Python27\lib\encodings\cp1255.py to encodings\cp1255.pyc
byte-compiling C:\Python27\lib\encodings\cp1256.py to encodings\cp1256.pyc
byte-compiling C:\Python27\lib\encodings\cp1257.py to encodings\cp1257.pyc
byte-compiling C:\Python27\lib\encodings\cp1258.py to encodings\cp1258.pyc
byte-compiling C:\Python27\lib\encodings\cp424.py to encodings\cp424.pyc
byte-compiling C:\Python27\lib\encodings\cp437.py to encodings\cp437.pyc
byte-compiling C:\Python27\lib\encodings\cp500.py to encodings\cp500.pyc
byte-compiling C:\Python27\lib\encodings\cp720.py to encodings\cp720.pyc
byte-compiling C:\Python27\lib\encodings\cp737.py to encodings\cp737.pyc
byte-compiling C:\Python27\lib\encodings\cp775.py to encodings\cp775.pyc
byte-compiling C:\Python27\lib\encodings\cp850.py to encodings\cp850.pyc
byte-compiling C:\Python27\lib\encodings\cp852.py to encodings\cp852.pyc
byte-compiling C:\Python27\lib\encodings\cp855.py to encodings\cp855.pyc
byte-compiling C:\Python27\lib\encodings\cp856.py to encodings\cp856.pyc
byte-compiling C:\Python27\lib\encodings\cp857.py to encodings\cp857.pyc
byte-compiling C:\Python27\lib\encodings\cp858.py to encodings\cp858.pyc
byte-compiling C:\Python27\lib\encodings\cp860.py to encodings\cp860.pyc
byte-compiling C:\Python27\lib\encodings\cp861.py to encodings\cp861.pyc
byte-compiling C:\Python27\lib\encodings\cp862.py to encodings\cp862.pyc
byte-compiling C:\Python27\lib\encodings\cp863.py to encodings\cp863.pyc
byte-compiling C:\Python27\lib\encodings\cp864.py to encodings\cp864.pyc
byte-compiling C:\Python27\lib\encodings\cp865.py to encodings\cp865.pyc
byte-compiling C:\Python27\lib\encodings\cp866.py to encodings\cp866.pyc
byte-compiling C:\Python27\lib\encodings\cp869.py to encodings\cp869.pyc
byte-compiling C:\Python27\lib\encodings\cp874.py to encodings\cp874.pyc
byte-compiling C:\Python27\lib\encodings\cp875.py to encodings\cp875.pyc
byte-compiling C:\Python27\lib\encodings\cp932.py to encodings\cp932.pyc
byte-compiling C:\Python27\lib\encodings\cp949.py to encodings\cp949.pyc
byte-compiling C:\Python27\lib\encodings\cp950.py to encodings\cp950.pyc
byte-compiling C:\Python27\lib\encodings\euc_jis_2004.py to encodings\euc_jis_2004.pyc
byte-compiling C:\Python27\lib\encodings\euc_jisx0213.py to encodings\euc_jisx0213.pyc
byte-compiling C:\Python27\lib\encodings\euc_jp.py to encodings\euc_jp.pyc
byte-compiling C:\Python27\lib\encodings\euc_kr.py to encodings\euc_kr.pyc
byte-compiling C:\Python27\lib\encodings\gb18030.py to encodings\gb18030.pyc
byte-compiling C:\Python27\lib\encodings\gb2312.py to encodings\gb2312.pyc
byte-compiling C:\Python27\lib\encodings\gbk.py to encodings\gbk.pyc
byte-compiling C:\Python27\lib\encodings\hex_codec.py to encodings\hex_codec.pyc
byte-compiling C:\Python27\lib\encodings\hp_roman8.py to encodings\hp_roman8.pyc
byte-compiling C:\Python27\lib\encodings\hz.py to encodings\hz.pyc
byte-compiling C:\Python27\lib\encodings\idna.py to encodings\idna.pyc
byte-compiling C:\Python27\lib\encodings\iso2022_jp.py to encodings\iso2022_jp.pyc
byte-compiling C:\Python27\lib\encodings\iso2022_jp_1.py to encodings\iso2022_jp_1.pyc
byte-compiling C:\Python27\lib\encodings\iso2022_jp_2.py to encodings\iso2022_jp_2.pyc
byte-compiling C:\Python27\lib\encodings\iso2022_jp_2004.py to encodings\iso2022_jp_2004.pyc
byte-compiling C:\Python27\lib\encodings\iso2022_jp_3.py to encodings\iso2022_jp_3.pyc
byte-compiling C:\Python27\lib\encodings\iso2022_jp_ext.py to encodings\iso2022_jp_ext.pyc
byte-compiling C:\Python27\lib\encodings\iso2022_kr.py to encodings\iso2022_kr.pyc
byte-compiling C:\Python27\lib\encodings\iso8859_1.py to encodings\iso8859_1.pyc
byte-compiling C:\Python27\lib\encodings\iso8859_10.py to encodings\iso8859_10.pyc
byte-compiling C:\Python27\lib\encodings\iso8859_11.py to encodings\iso8859_11.pyc
byte-compiling C:\Python27\lib\encodings\iso8859_13.py to encodings\iso8859_13.pyc
byte-compiling C:\Python27\lib\encodings\iso8859_14.py to encodings\iso8859_14.pyc
byte-compiling C:\Python27\lib\encodings\iso8859_15.py to encodings\iso8859_15.pyc
byte-compiling C:\Python27\lib\encodings\iso8859_16.py to encodings\iso8859_16.pyc
byte-compiling C:\Python27\lib\encodings\iso8859_2.py to encodings\iso8859_2.pyc
byte-compiling C:\Python27\lib\encodings\iso8859_3.py to encodings\iso8859_3.pyc
byte-compiling C:\Python27\lib\encodings\iso8859_4.py to encodings\iso8859_4.pyc
byte-compiling C:\Python27\lib\encodings\iso8859_5.py to encodings\iso8859_5.pyc
byte-compiling C:\Python27\lib\encodings\iso8859_6.py to encodings\iso8859_6.pyc
byte-compiling C:\Python27\lib\encodings\iso8859_7.py to encodings\iso8859_7.pyc
byte-compiling C:\Python27\lib\encodings\iso8859_8.py to encodings\iso8859_8.pyc
byte-compiling C:\Python27\lib\encodings\iso8859_9.py to encodings\iso8859_9.pyc
byte-compiling C:\Python27\lib\encodings\johab.py to encodings\johab.pyc
byte-compiling C:\Python27\lib\encodings\koi8_r.py to encodings\koi8_r.pyc
byte-compiling C:\Python27\lib\encodings\koi8_u.py to encodings\koi8_u.pyc
byte-compiling C:\Python27\lib\encodings\latin_1.py to encodings\latin_1.pyc
byte-compiling C:\Python27\lib\encodings\mac_arabic.py to encodings\mac_arabic.pyc
byte-compiling C:\Python27\lib\encodings\mac_centeuro.py to encodings\mac_centeuro.pyc
byte-compiling C:\Python27\lib\encodings\mac_croatian.py to encodings\mac_croatian.pyc
byte-compiling C:\Python27\lib\encodings\mac_cyrillic.py to encodings\mac_cyrillic.pyc
byte-compiling C:\Python27\lib\encodings\mac_farsi.py to encodings\mac_farsi.pyc
byte-compiling C:\Python27\lib\encodings\mac_greek.py to encodings\mac_greek.pyc
byte-compiling C:\Python27\lib\encodings\mac_iceland.py to encodings\mac_iceland.pyc
byte-compiling C:\Python27\lib\encodings\mac_latin2.py to encodings\mac_latin2.pyc
byte-compiling C:\Python27\lib\encodings\mac_roman.py to encodings\mac_roman.pyc
byte-compiling C:\Python27\lib\encodings\mac_romanian.py to encodings\mac_romanian.pyc
byte-compiling C:\Python27\lib\encodings\mac_turkish.py to encodings\mac_turkish.pyc
byte-compiling C:\Python27\lib\encodings\mbcs.py to encodings\mbcs.pyc
byte-compiling C:\Python27\lib\encodings\palmos.py to encodings\palmos.pyc
byte-compiling C:\Python27\lib\encodings\ptcp154.py to encodings\ptcp154.pyc
byte-compiling C:\Python27\lib\encodings\punycode.py to encodings\punycode.pyc
byte-compiling C:\Python27\lib\encodings\quopri_codec.py to encodings\quopri_codec.pyc
byte-compiling C:\Python27\lib\encodings\raw_unicode_escape.py to encodings\raw_unicode_escape.pyc
byte-compiling C:\Python27\lib\encodings\rot_13.py to encodings\rot_13.pyc
byte-compiling C:\Python27\lib\encodings\shift_jis.py to encodings\shift_jis.pyc
byte-compiling C:\Python27\lib\encodings\shift_jis_2004.py to encodings\shift_jis_2004.pyc
byte-compiling C:\Python27\lib\encodings\shift_jisx0213.py to encodings\shift_jisx0213.pyc
byte-compiling C:\Python27\lib\encodings\string_escape.py to encodings\string_escape.pyc
byte-compiling C:\Python27\lib\encodings\tis_620.py to encodings\tis_620.pyc
byte-compiling C:\Python27\lib\encodings\undefined.py to encodings\undefined.pyc
byte-compiling C:\Python27\lib\encodings\unicode_escape.py to encodings\unicode_escape.pyc
byte-compiling C:\Python27\lib\encodings\unicode_internal.py to encodings\unicode_internal.pyc
byte-compiling C:\Python27\lib\encodings\utf_16.py to encodings\utf_16.pyc
byte-compiling C:\Python27\lib\encodings\utf_16_be.py to encodings\utf_16_be.pyc
byte-compiling C:\Python27\lib\encodings\utf_16_le.py to encodings\utf_16_le.pyc
byte-compiling C:\Python27\lib\encodings\utf_32.py to encodings\utf_32.pyc
byte-compiling C:\Python27\lib\encodings\utf_32_be.py to encodings\utf_32_be.pyc
byte-compiling C:\Python27\lib\encodings\utf_32_le.py to encodings\utf_32_le.pyc
byte-compiling C:\Python27\lib\encodings\utf_7.py to encodings\utf_7.pyc
byte-compiling C:\Python27\lib\encodings\utf_8.py to encodings\utf_8.pyc
byte-compiling C:\Python27\lib\encodings\utf_8_sig.py to encodings\utf_8_sig.pyc
byte-compiling C:\Python27\lib\encodings\uu_codec.py to encodings\uu_codec.pyc
byte-compiling C:\Python27\lib\encodings\zlib_codec.py to encodings\zlib_codec.pyc
byte-compiling C:\Python27\lib\fnmatch.py to fnmatch.pyc
byte-compiling C:\Python27\lib\functools.py to functools.pyc
byte-compiling C:\Python27\lib\genericpath.py to genericpath.pyc
byte-compiling C:\Python27\lib\getopt.py to getopt.pyc
byte-compiling C:\Python27\lib\gettext.py to gettext.pyc
byte-compiling C:\Python27\lib\heapq.py to heapq.pyc
byte-compiling C:\Python27\lib\inspect.py to inspect.pyc
byte-compiling C:\Python27\lib\keyword.py to keyword.pyc
byte-compiling C:\Python27\lib\linecache.py to linecache.pyc
byte-compiling C:\Python27\lib\locale.py to locale.pyc
byte-compiling C:\Python27\lib\ntpath.py to ntpath.pyc
byte-compiling C:\Python27\lib\opcode.py to opcode.pyc
byte-compiling C:\Python27\lib\optparse.py to optparse.pyc
byte-compiling C:\Python27\lib\os.py to os.pyc
byte-compiling C:\Python27\lib\os2emxpath.py to os2emxpath.pyc
byte-compiling C:\Python27\lib\pdb.py to pdb.pyc
byte-compiling C:\Python27\lib\pickle.py to pickle.pyc
byte-compiling C:\Python27\lib\posixpath.py to posixpath.pyc
byte-compiling C:\Python27\lib\pprint.py to pprint.pyc
byte-compiling C:\Python27\lib\quopri.py to quopri.pyc
byte-compiling C:\Python27\lib\random.py to random.pyc
byte-compiling C:\Python27\lib\re.py to re.pyc
byte-compiling C:\Python27\lib\repr.py to repr.pyc
byte-compiling C:\Python27\lib\shlex.py to shlex.pyc
byte-compiling C:\Python27\lib\sre.py to sre.pyc
byte-compiling C:\Python27\lib\sre_compile.py to sre_compile.pyc
byte-compiling C:\Python27\lib\sre_constants.py to sre_constants.pyc
byte-compiling C:\Python27\lib\sre_parse.py to sre_parse.pyc
byte-compiling C:\Python27\lib\stat.py to stat.pyc
byte-compiling C:\Python27\lib\string.py to string.pyc
byte-compiling C:\Python27\lib\stringprep.py to stringprep.pyc
byte-compiling C:\Python27\lib\struct.py to struct.pyc
byte-compiling C:\Python27\lib\subprocess.py to subprocess.pyc
byte-compiling C:\Python27\lib\tempfile.py to tempfile.pyc
byte-compiling C:\Python27\lib\textwrap.py to textwrap.pyc
byte-compiling C:\Python27\lib\threading.py to threading.pyc
byte-compiling C:\Python27\lib\token.py to token.pyc
byte-compiling C:\Python27\lib\tokenize.py to tokenize.pyc
byte-compiling C:\Python27\lib\traceback.py to traceback.pyc
byte-compiling C:\Python27\lib\types.py to types.pyc
byte-compiling C:\Python27\lib\unittest\__init__.py to unittest\__init__.pyc
creating E:\python\build\bdist.win32\winexe\collect-2.7\unittest
byte-compiling C:\Python27\lib\unittest\case.py to unittest\case.pyc
byte-compiling C:\Python27\lib\unittest\loader.py to unittest\loader.pyc
byte-compiling C:\Python27\lib\unittest\main.py to unittest\main.pyc
byte-compiling C:\Python27\lib\unittest\result.py to unittest\result.pyc
byte-compiling C:\Python27\lib\unittest\runner.py to unittest\runner.pyc
byte-compiling C:\Python27\lib\unittest\signals.py to unittest\signals.pyc
byte-compiling C:\Python27\lib\unittest\suite.py to unittest\suite.pyc
byte-compiling C:\Python27\lib\unittest\util.py to unittest\util.pyc
byte-compiling C:\Python27\lib\warnings.py to warnings.pyc
byte-compiling C:\Python27\lib\weakref.py to weakref.pyc
byte-compiling E:\python\build\bdist.win32\winexe\temp\bz2.py to bz2.pyc
byte-compiling E:\python\build\bdist.win32\winexe\temp\select.py to select.pyc
byte-compiling E:\python\build\bdist.win32\winexe\temp\unicodedata.py to unicodedata.pyc
*** copy extensions ***
copying C:\Python27\DLLs\bz2.pyd -> E:\python\dist
copying C:\Python27\DLLs\select.pyd -> E:\python\dist
copying C:\Python27\DLLs\unicodedata.pyd -> E:\python\dist
*** copy dlls ***
copying C:\Python27\w9xpopen.exe -> E:\python\dist
copying C:\Windows\system32\python27.dll -> E:\python\dist
setting sys.winver for 'E:\python\dist\python27.dll' to 'py2exe'
copying C:\Python27\lib\site-packages\py2exe\run.exe -> E:\python\dist\hello.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

E:\python>
此时在同目录下有两个文件夹,其中可执行文件就在其中:

Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。

C:\Users\sean>e:

E:\>cd python

E:\python>dir
驱动器 E 中的卷是 新加卷
卷的序列号是 B695-87CE

E:\python 的目录

2017/06/21 18:12 <DIR> .
2017/06/21 18:12 <DIR> ..
2017/06/21 18:12 <DIR> build
2017/06/21 18:14 <DIR> dist
2017/06/21 18:07 20 hello.py
2017/06/21 18:08 78 setup.py
2 个文件 98 字节
4 个目录 104,372,019,200 可用字节

E:\python>cd dist

E:\python\dist>dir
驱动器 E 中的卷是 新加卷
卷的序列号是 B695-87CE

E:\python\dist 的目录

2017/06/21 18:14 <DIR> .
2017/06/21 18:14 <DIR> ..
2010/07/04 09:03 71,680 bz2.pyd
2017/06/21 18:12 18,944 hello.exe
2017/06/21 18:12 1,506,260 library.zip
2010/07/04 09:03 2,286,080 python27.dll
2010/07/04 09:04 11,776 select.pyd
2010/07/04 09:03 688,128 unicodedata.pyd
2010/07/04 09:01 49,664 w9xpopen.exe
7 个文件 4,632,532 字节
2 个目录 104,372,019,200 可用字节

E:\python\dist>hello.exe
Hello World!

E:\python\dist>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: