用Py2exe打包Python脚本简单介绍

一、简述
      Py2exe,从这个名字上就可以理解,把Python脚本转换为windows平台上面可以运行的可执行程序(*.exe)的工具。经过转换后,你可以不

用安装Python的执行环境就可以直接执行转换后的exe了。Py2exe本身是开源的。


二、安装
      根据你本地安装的python的版本来选择要安装的Py2exe版本,一个比较好的下载地址:http://sourceforge.net/projects/py2exe/files/,如果这个地址访问不了,我在csdn上放了一个针对python2.5的Py2exe安装包,可以去下载:http://d.download.csdn.net/down/2793052/magictong

三、使用

      看一个简单的例子:先写一个简单的脚本,文件名:HelloPy2exe.py

[python] view plaincopy
 
  1. #!/usr/bin/env python  
  2. # -*- coding: utf-8 -*-  
  3. # Created By MagicTong 2010-11-01 13:01  
  4.   
  5. def main():  
  6.     print "Hello, Py2exe! "  
  7.   
  8. if __name__ == "__main__":  
  9.     main()  
  10.     raw_input("Enter enter key to exit...")  

      然后写一个编译脚本,文件名:setup_py2exe.py

[python] view plaincopy
 
  1. from distutils.core import setup  
  2. import py2exe  
  3.   
  4. setup(console=["HelloPy2exe.py"])  

      最后整个批处理,文件名:buildExe.bat

[python] view plaincopy
 
  1. python setup_py2exe.py py2exe  
  2. pause  

      运行这个批处理后,会在当前目录生成两个文件夹:build和dist,build里面的是一些中间文件可以不用理会了,dist里面会产生下面一些文件:bz2.pyd,HelloPy2exe.exe,library.zip,MSVCR71.dll,python25.dll,unicodedata.pyd,w9xpopen.exe,这时你可以在一台没有安装python环境的机器上运行HelloPy2exe.exe了:

     

四、发布

      dist里面的所有文件都需要发布的,但是如果你不需要兼容win9系列,那可以不带上w9xpopen.exe,其实你直接运行下w9xpopen.exe会提示:

五、Py2exe支持的参数(通过python setup_py2exe.py py2exe --help 可以打印出来,但是这些参数我也没用过,有空再摸索摸索)

[python] view plaincopy
 
  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 1, 2, or 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  

六、高级,其实也不高级

      看那个编译脚本中的这句:setup(console=["HelloPy2exe.py"]),setup还支持很多参数,windows(一个windows界面程序),data_filse(打包其他的文件)……以后再说说。

      看一个例子先:

[python] view plaincopy
 
  1. # -*- coding: cp936 -*-  
  2. from distutils.core import setup  
  3. import py2exe  
  4.   
  5. includes = ["encodings", "encodings.*"]  
  6.   
  7. options = {"py2exe":    
  8.             {"compressed": 1, #压缩    
  9.              "optimize": 2,    
  10.              "ascii": 1,    
  11.              "includes":includes,    
  12.              "bundle_files": #所有文件打包成一个exe文件  
  13.             }}  
  14. setup(  
  15.     options=options,    
  16.     zipfile=None,  
  17.     console=[{"script": "HelloPy2exe.py", "icon_resources": [(1, "pc.ico")]}],  
  18.     windows=[{"script": "HelloWin.py", "icon_resources": [(1, "pc.ico")]}],  
  19.     data_files=[("magic",["App_x86.exe",]),],  
  20.     version = "2010.11.01.01",   
  21.     description = "this is a py2exe test",   
  22.     name = "HelloGuys.",   
  23. )  

      options可以用来指定一些编译的参数,譬如是否压缩,是否打包为一个文件,data_files是一个打包时的拷贝文件列表,格式如下:data_files=[("目的文件夹",["文件名",]), ("目的文件夹",["文件名",]), ("目的文件夹",["文件名",]),],至于version,description,name等等,你们懂的,icon_resources是指定一个ico图标作为程序的图标。从这里也可以看出windows,console等参数是可以指定一个list来设置参数的。可以去Python安装目录/Lib/site-packages/py2exe/samples下看一些例子,这玩意还可以打包服务程序,com服务器程序等等。

      王道:http://www.py2exe.org/

[end]

原文地址:https://www.cnblogs.com/jinjiangongzuoshi/p/4100852.html