py2exe py2exe是什么? py2exe是一種python發(fā)布工具,可以把python腳本轉(zhuǎn)換成windows下的可執(zhí)行程序,不需要安裝python便可運(yùn)行。 py2exe現(xiàn)在可以用來(lái)創(chuàng)建使用了wxPython, Tkinter, Pmw, PyGTK, pygame, win32com client and server 等模塊的程序。 詳細(xì)介紹可以看它的官方網(wǎng)站 http://starship./crew/theller/py2exe/ 1. 下載安裝py2exe py2exe目前的版本是0.5.4,根據(jù)你安裝的python的版本選擇下載的文件 py2exe-0.5.4.win32-py2.3.exe 或 py2exe-0.5.4.win32-py2.4.exe 安裝后的文件應(yīng)該在你的python安裝目錄下的Lib\site-packages\py2exe 2. 使用py2exe 我們先準(zhǔn)備一個(gè)簡(jiǎn)單的python程序hello.py 代碼: # hello.py def main(): print "Hello, World!" if __name__ == ‘__main__‘: main() 然后為使用py2exe寫一個(gè)腳本setup.py 代碼: # setup.py from distutils.core import setup import py2exe setup(console=["hello.py"]) 運(yùn)行setup.py,記得要傳一個(gè)參數(shù)給它 代碼: python setup.py py2exe 應(yīng)該看到一些輸出信息 引用: running py2exe creating E:\Projects\WorkSpace\Python\build creating E:\Projects\WorkSpace\Python\build\bdist.win32 creating E:\Projects\WorkSpace\Python\build\bdist.win32\winexe creating E:\Projects\WorkSpace\Python\build\bdist.win32\winexe\collect creating E:\Projects\WorkSpace\Python\build\bdist.win32\winexe\temp creating 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.pyc byte-compiling C:\Python23\lib\sre_compile.py to sre_compile.pyc byte-compiling E:\Projects\WorkSpace\Python\build\bdist.win32\winexe\temp\_sre.py to _sre.pyc byte-compiling C:\Python23\lib\macpath.py to macpath.pyc byte-compiling C:\Python23\lib\popen2.py to popen2.pyc byte-compiling C:\Python23\lib\atexit.py to atexit.pyc byte-compiling C:\Python23\lib\os2emxpath.py to os2emxpath.pyc byte-compiling C:\Python23\lib\sre_constants.py to sre_constants.pyc byte-compiling C:\Python23\lib\re.py to re.pyc byte-compiling C:\Python23\lib\ntpath.py to ntpath.pyc byte-compiling C:\Python23\lib\stat.py to stat.pyc byte-compiling C:\Python23\lib\string.py to string.pyc byte-compiling C:\Python23\lib\warnings.py to warnings.pyc byte-compiling C:\Python23\lib\UserDict.py to UserDict.pyc byte-compiling C:\Python23\lib\repr.py to repr.pyc byte-compiling C:\Python23\lib\copy.py to copy.pyc byte-compiling C:\Python23\lib\types.py to types.pyc byte-compiling C:\Python23\lib\posixpath.py to posixpath.pyc byte-compiling C:\Python23\lib\sre.py to sre.pyc byte-compiling C:\Python23\lib\linecache.py to linecache.pyc byte-compiling C:\Python23\lib\sre_parse.py to sre_parse.pyc byte-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會(huì)在當(dāng)前目錄下生成兩個(gè)目錄 build和dist build里是一些py2exe運(yùn)行時(shí)產(chǎn)生的中間文件,dist里有最終的可執(zhí)行文件 library.zip w9xpopen.exe python23.dll hello.exe 現(xiàn)在可以運(yùn)行hello.exe了 代碼: E:\Projects\WorkSpace\Python\dist>hello Hello, World! 不過(guò)記得如果要發(fā)布到別的機(jī)器上時(shí),library.zip、 w9xpopen.exe、python23.dll這幾個(gè)文件是必須要和hello.exe在一起的。 好了,這次先到這里,下次我們做一個(gè)wxPython的例子 最后,大家試試運(yùn)行 代碼: python setup.py py2exe --help 看看py2exe都有哪些參數(shù) |
|