py2exe使用方法 (含一些調試技巧,如壓縮email 類)

一、簡介

 

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 。


mysetup.py示例如下:

 

 

Python代碼  收藏代碼
  1. from distutils.core import setup  
  2. import py2exe  
  3.   
  4. setup(console=["helloworld.py"])   

 

 

如果顯示錯誤提示的話 “ msvcp90.dll: no such file or directory”

 

請嘗試下面的方法:

 

Python代碼  收藏代碼
  1. from distutils.core import setup  
  2. import py2exe  
  3.   
  4. setup(  
  5.     console=["helloworld.py"],  
  6.     options = { "py2exe": { "dll_excludes": ["MSVCP90.dll"] } }  
  7. )  
 

然後按下面的方法運行mysetup.py: (dos:  cmd => cd desktop => mysetup.py py2exe)
python mysetup.py py2exe


上面的命令執行後將產生一個名爲dist的子目錄,其中包含了helloworld.exe,python24.dll,library.zip這些文件。
如果你的helloworld.py腳本中用了已編譯的C擴展模塊,那麼這些模塊也會被拷貝在個子目錄中,同樣,所有的dll文件在運行時都是需要的,除了系統的dll文件。


dist子目錄中的文件包含了你的程序所必須的東西,你應將這個子目錄中的所有內容一起發佈。

默認情況下,py2exe在目錄dist下創建以下這些必須的文件:
1、一個或多個exe文件。
2、python##.dll。 
3、幾個.pyd文件,它們是已編譯的擴展名,它們是exe文件所需要的;加上其它的.dll文件,這些.dll是.pyd所需要的。
4、一個library.zip文件,它包含了已編譯的純的python模塊如.pyc或.pyo


上面的mysetup.py創建了一個控制檯的helloword.exe程序,如果你要創建一個圖形用戶界的程序,那麼你只需要將mysetup.py中的console=["helloworld.py"]替換爲windows=["myscript.py"]既可。

 

py2exe一次能夠創建多個exe文件,你需要將這些腳本文件的列表傳遞給console或windows的關鍵字參數。如果你有幾個相關聯的腳本,那麼這是很有用的。


運行下面個命令,將顯示py2exe命令的所有命令行標記。
python mysetup.py py2exe --help

 

Python代碼  收藏代碼
  1. Global options:  
  2.   --verbose (-v)  run verbosely (default)  
  3.   --quiet (-q)    run quietly (turns verbosity off)  
  4.   --dry-run (-n)  don't actually do anything  
  5.   --help (-h)     show detailed help message  
  6.   
  7. Options for 'py2exe' command:  
  8.   --optimize (-O)       optimization level: -O1 for "python -O", -O2 for  
  9.                         "python -OO"and -O0 to disable [default: -O0]  
  10.   --dist-dir (-d)       directory to put final built distributions in (default  
  11.                         is dist)  
  12.   --excludes (-e)       comma-separated list of modules to exclude  
  13.   --dll-excludes        comma-separated list of DLLs to exclude  
  14.   --ignores             comma-separated list of modules to ignore if they are  
  15.                         not found  
  16.   --includes (-i)       comma-separated list of modules to include  
  17.   --packages (-p)       comma-separated list of packages to include  
  18.   --compressed (-c)     create a compressed zipfile  
  19.   --xref (-x)           create and show a module cross reference  
  20.   --bundle-files (-b)   bundle dlls in the zipfile or the exe. Valid levels  
  21.                         are 12or 3 (default)  
  22.   --skip-archive        do not place Python bytecode files in an archive, put  
  23.                         them directly in the file system  
  24.   --ascii (-a)          do not automatically include encodings and codecs  
  25.   --custom-boot-script  Python file that will be run when setting up the  
  26.                         runtime environment  
  27.   
  28. usage: setup_py2exe.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]  
  29.    or: setup_py2exe.py --help [cmd1 cmd2 ...]  
  30.    or: setup_py2exe.py --help-commands  
  31.    or: setup_py2exe.py cmd --help  
 


四、指定額外的文件

 

一些應用程序在運行時需要額外的文件,諸如配置文件、字體、位圖。
如果在安裝腳本中用data_files可選項指定了那些額外的文件,那麼py2exe能將這些文件拷貝到dist子目錄中。data_files應包含一個元組(target-dir, files)列表,其中的files是這些額外的文件的列表。


示例如下:

 

PythonCode:  # mysetup.py

 

Python代碼  收藏代碼
  1. from distutils.core import setup  
  2. import glob  
  3. import py2exe  
  4.   
  5. setup(console=["helloworld.py"],  
  6.       data_files=[("bitmaps",  
  7.                    ["bm/large.gif""bm/small.gif"]),  
  8.                   ("fonts",  
  9.                    glob.glob("fonts\\*.fnt"))],  
  10. )   
 

說明:data_files選項將創建一個子目錄dist\bitmaps,其中包含兩個.gif文件;一個子目錄dist\fonts,其中包含了所有的.fnt文件。

 

 

五、Windows NT services

 

你可以通過傳遞一個service關鍵字參數給setup函數來建造Windows NT services
,這個service參數的值必須是一個Python模塊名(包含一service類)的列表。


示例如下:

PythonCode:  # mysetup.py

 

Python代碼  收藏代碼
  1. from distutils.core import setup  
  2. import py2exe  
  3.   
  4. setup(service=["MyService"])   
 

所建造的可執行的service是可以通過在其後跟一定的命令行參數標記來自行安裝和卸載的。你可以通過在這個可執行的service(exe)後跟一-help參數來得到更多的幫助。

 


六、COM servers

 

你可以通過傳遞一個com_server 關鍵字參數給setup函數來建造Windows NT services
,這個service參數的值必須是一個Python模塊名(包含一個或多個COM server 類)的列表。


示例如下:

PythonCode:  # mysetup.py

 

Python代碼  收藏代碼
  1. from distutils.core import setup  
  2. import py2exe  
  3.   
  4. setup(com_server=["win32com.server.interp"])   
 

 

默認情況下,DLL和EXE servers被建造,你不需要它們的話你可以簡單的刪除它們。

一個標準的py2exe setup文件編寫

 

 

Python代碼  收藏代碼
  1.  -*- coding: cp936 -*-  
  2. from distutils.core import setup  
  3.   
  4. import py2exe  
  5.   
  6.   
  7. includes = ["encodings""encodings.*"]     
  8. #要包含的其它庫文件  
  9.   
  10. options = {"py2exe":  
  11.   
  12.     {"compressed"1#壓縮  
  13.      "optimize"2,  
  14.      "ascii"1,  
  15.      "includes":includes,  
  16.      "bundle_files"1 #所有文件打包成一個exe文件 }  
  17.     }  
  18. setup(      
  19.     options = options,       
  20.     zipfile=None,   #不生成library.zip文件  
  21.     console=[{"script""hello.py""icon_resources": [(1"hello.ico")] }]#源文件,程序圖標  
  22.     )   
 

 

新 版本已經可以打包爲一個文件了,以前都是一堆dll,pyd的。具體的變化其實只有一個地方。就是options裏增加bundle_files項,值爲 1表示pyd和dll文件會被打包到exe文件中,且不能從文件系統中加載python模塊;值爲2表示pyd和dll文件會被打包到exe文件中,但是 可以從文件系統中加載python模塊。另外setup中使用zipfile=None可以不生成library.zip。

 

例如原來 的:

 

Python代碼  收藏代碼
  1. from distutils.core import setup   
  2. import py2exe   
  3. includes = ["encodings""encodings.*"]   
  4. options = {"py2exe":   
  5.             {   "compressed"1,   
  6.                 "optimize"2,   
  7.                 "includes": includes,                   
  8.             }   
  9.           }   
  10. setup(      
  11.     version = "0.1.0",   
  12.     description = "search panda",   
  13.     name = "search panda",       
  14.     options = options,       
  15.     windows=[{"script""search.py""icon_resources": [(1"search.ico")] }],         
  16.     )  
 

 

只需要改爲: 

 

Python代碼  收藏代碼
  1. from distutils.core import setup   
  2. import py2exe   
  3. includes = ["encodings""encodings.*"]   
  4. options = {"py2exe":   
  5.             {   "compressed"1,   
  6.                 "optimize"2,   
  7.                 "includes": includes,   
  8.                 "bundle_files"1   
  9.             }   
  10.           }   
  11. setup(      
  12.     version = "0.1.0",   
  13.     description = "search panda",   
  14.     name = "search panda",   
  15.     options = options,   
  16.     zipfile=None,   
  17.     windows=[{"script""search.py""icon_resources": [(1"search.ico")] }],     
  18.        
  19.     )   
 

 

 

比如,這裏我打包以前的DelphiCode2HTML的

 

Python代碼  收藏代碼
  1. # -*- coding: gbk -*-  
  2.   
  3. from distutils.core import setup  
  4. import py2exe  
  5.   
  6. includes = ["encodings""encodings.*"]  
  7. options = {"py2exe":  
  8.                     {"compressed"1,  
  9.                      "optimize"2,  
  10.                      "ascii"1,  
  11.                      "includes":includes,  
  12.                      "bundle_files"1}  
  13.            }  
  14. setup(  
  15.     options = options,  
  16.     zipfile=None,  
  17.     name = "HelloGuys.",  
  18.     description = "this is a py2exe test",     
  19.     windows=[{"script""F:\我的程序\Python\CSDN Code Edit\Code2Html.py",  
  20.               "icon_resources": [(1"F:\書籍\我的圖標\圖標xp\Convert.ico")]  
  21.               }]  
  22.     )  
 

 

下面列出他的一些 options

 

keyword

  description

data_files

  list of "data" files that you are going to need to run your executable such as .pngs, .jpgs

 

 

Py2exe extends Distutils setup keywords

 

In addition to the standard distutils setup keywords, the following py2exe keywords specify what and how to build.

keyword

description

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'. If zipfile is set toNone , the files will be bundled within the executable instead of 'library.zip'.

options

dictionary { "py2exe": { "opt1": val1, "opt2": val2, ... } }

 

 

The options dictionary of py2exe

 

The option keyword takes the following set of dictionary key: value pairs. The dictionary "key" names and the "value" types are listed in the table below.

 

key

value

unbuffered

if true, use unbuffered binary stdout and stderr

optimize

string or int of optimization level (0, 1, or 2) 0 = don’t optimize (generate .pyc) 1 = normal optimization (like python -O) 2 = extra optimization (like python -OO) See http://docs.python.org/distutils/apiref.html#module-distutils.util for more info.

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 in which to build the final files

typelibs

list of gen_py generated typelibs to include

compressed

(boolean) create a compressed zipfile

xref

(boolean) create and show a module cross reference

bundle_files

bundle dlls in the zipfile or the exe. Valid values for bundle_files are: 3 = don't bundle (default) 2 = bundle everything but the Python interpreter 1 = bundle everything, including the Python interpreter

skip_archive

(boolean) do not place Python bytecode files in an archive, put them directly in the file system

ascii

(boolean) do not automatically include encodings and codecs

custom-boot-script

Python file that will be run when setting up the runtime environment

 

 

 

Example:

 

Python代碼  收藏代碼
  1. setup(  
  2.         windows=['trypyglet.py'],  
  3.         options={  
  4.                 "py2exe":{  
  5.                         "unbuffered"True,  
  6.                         "optimize"2,  
  7.                         "excludes": ["email"]  
  8.                 }  
  9.         }  
  10. )   

 

 

 

For more information enter the following at the python command line:

 

 

Python代碼  收藏代碼
  1. >>> from distutils.core import setup  
  2. >>> help(setup)  
 

注意 windows 的用法,他可以代替 console, 如果你要集成 wxpython 的時候,一定會用的 !

 

 

更多請查看 http://www.py2exe.org/index.cgi/ListOfOptions

 

 

如果程序中含有email類,並且壓縮時出現類似 “ImportError: No module named multipart ” 的錯誤,你需要如下的設置:

 

 

1. 嘗試將Lib下的email包,複製到當前文件夾中

2. 把['emai'] 放入includes中

3. 把['email']放入packages中

4. 繼續運行py2exe

 

如:

 

Python代碼  收藏代碼
  1. from distutils.core import setup   
  2. import py2exe  
  3.   
  4. includes = ["encodings""encodings.*",'email']  
  5.   
  6. options = {"py2exe":   
  7.             {   "compressed"1,   
  8.                 "optimize"2,   
  9.                 "includes": includes,   
  10.                 "bundle_files"1,  
  11.                 "packages": ['email'],  
  12.                 "dll_excludes": ["MSVCP90.dll"]  
  13.             }   
  14.           }   
  15. setup(      
  16.     version = "0.1.0",   
  17.     description = "3th",   
  18.     name = "For My Lover",   
  19.     options = options,   
  20.     zipfile=None,   
  21.     windows=[{"script""love.py""icon_resources": [(1"roses.ico")] }],     
  22.     )   
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章