用py2exe 创建可执行文件

来源:互联网 发布:linux 压缩与解压 编辑:程序博客网 时间:2024/05/16 14:49
py2exe是一种python发布工具,可以把python脚本转换成windows下的可执行程序,不需要安装python便可运行。py2exe现在可以用来创建使用了wxPython, Tkinter, Pmw,PyGTK, pygame, win32com client and server 等模块的程序。

详细介绍可以看它的官方网站 http://www.py2exe.org/


py2exe目前的版本是0.6.9,根据你安装的python的版本选择下载的文件

http://sourceforge.net/projects/py2exe/files/py2exe/0.6.9/

下载后,直接运行安装就行了,安装后的文件应该在你的python安装目录下的Lib\site-packages\py2exe


我们先准备一个简单的python程序hello.py

# hello.pydef main():    print "Hello, World!"if __name__ == '__main__':   main() 

然后为使用py2exe写一个脚本setup.py

# setup.pyfrom distutils.core import setupimport py2exe       setup(console=["hello.py"])

运行setup.py,记得要传一个参数给它

python setup.py py2exe

应该看到一些输出信息

creating E:\Projects\WorkSpace\Python\buildcreating E:\Projects\WorkSpace\Python\build\bdist.win32creating E:\Projects\WorkSpace\Python\build\bdist.win32\winexecreating E:\Projects\WorkSpace\Python\build\bdist.win32\winexe\collectcreating E:\Projects\WorkSpace\Python\build\bdist.win32\winexe\tempcreating E:\Projects\WorkSpace\Python\dist*** searching for required modules ****** parsing results ***creating python loader for extension '_sre'*** finding dlls needed ****** create binaries ****** byte compile python files ***byte-compiling C:\Python23\lib\copy_reg.py to copy_reg.pycbyte-compiling C:\Python23\lib\sre_compile.py to sre_compile.pycbyte-compiling E:\Projects\WorkSpace\Python\build\bdist.win32\winexe\temp\_sre.py to _sre.pycbyte-compiling C:\Python23\lib\macpath.py to macpath.pycbyte-compiling C:\Python23\lib\popen2.py to popen2.pycbyte-compiling C:\Python23\lib\atexit.py to atexit.pycbyte-compiling C:\Python23\lib\os2emxpath.py to os2emxpath.pycbyte-compiling C:\Python23\lib\sre_constants.py to sre_constants.pycbyte-compiling C:\Python23\lib\re.py to re.pycbyte-compiling C:\Python23\lib\ntpath.py to ntpath.pycbyte-compiling C:\Python23\lib\stat.py to stat.pycbyte-compiling C:\Python23\lib\string.py to string.pycbyte-compiling C:\Python23\lib\warnings.py to warnings.pycbyte-compiling C:\Python23\lib\UserDict.py to UserDict.pycbyte-compiling C:\Python23\lib\repr.py to repr.pycbyte-compiling C:\Python23\lib\copy.py to copy.pycbyte-compiling C:\Python23\lib\types.py to types.pycbyte-compiling C:\Python23\lib\posixpath.py to posixpath.pycbyte-compiling C:\Python23\lib\sre.py to sre.pycbyte-compiling C:\Python23\lib\linecache.py to linecache.pycbyte-compiling C:\Python23\lib\sre_parse.py to sre_parse.pycbyte-compiling C:\Python23\lib\os.py to os.pyc*** copy extensions ***copying C:\Python23\DLLs\_sre.pyd ->; E:\Projects\WorkSpace\Python\dist*** copy dlls ***

py2exe会在当前目录下生成两个目录 build和dist

build里是一些py2exe运行时产生的中间文件,dist里有最终的可执行文件

 library.zip python27.dll hello.exe

现在可以运行hello.exe了

E:\Projects\WorkSpace\Python\dist>helloHello, World!

python setup.py py2exe –help

看看py2exe都有哪些参数

原创粉丝点击