打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
将Python脚本文件包装成可执行文件

转自:http://blog.csdn.net/Jerry_1126/article/details/43908875

将Python脚本文件包装成可执行文件,其目的有二:

一则: 不需要依赖Python编译器就可以运行软件

二则: 不想让自己的源码公布出去

常用的工具有: py2exe、cx_freeze等

工具:py2exe

  • 安装py2exe

安装该工具很简单:

只需要从官方网站:http://www.py2exe.org/下载与版本对应的安装程序,点击下一步即可完成安装。

安装后,执行import py2exe,不报错则表示安装成功!

  1. >>> import py2exe  
  2. >>>  

NOTE: 目前该工具只支持到Python2.7, 对于Python3而言,必须借助另外一个工具:cx_freeze

  • 使用py2exe

第一步: 准备源代码,假如名为:Hello.py

第二步: 准备编译脚本,假如名为:setup.py

  1. from distutils.core import setup  
  2. import py2exe  
  3.   
  4. setup(windows=['Hello.py'])  

第三步: 运行命令: setup.py py2exe

  1. D:\temp>setup.py py2exe  
运行之后,会在我当前运行的目录下(D:\temp)默认生成dict目录,里面的文件如下:


  1. 默认情况下,py2exe在目录dist下创建以下这些必须的文件:  
  2. 1、一个或多个exe文件。如本例为: Hello.exe  
  3. 2、python##.dll。 如本例中: Python27.dll  
  4. 3、.pyd文件,它们是已编译的扩展名,它们是exe文件所需要的;加上其它的.dll文件,这些.dll是.pyd所需要的。  
  5. 4、一个library.zip文件,它包含了已编译的纯的python模块如.pyc或.pyo  


第四步: 双击Hello.exe可执行文件,跟源代码运行后同样的结果:


  • 其他

1: 执行setup.py --help获取帮助信息

  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.   --no-user-cfg   ignore pydistutils.cfg in your home directory  
  7.   
  8. Options for 'py2exe' command:  
  9.   --optimize (-O)       optimization level: -O1 for "python -O", -O2 for  
  10.                         "python -OO", and -O0 to disable [default: -O0]  
  11.   --dist-dir (-d)       directory to put final built distributions in (default  
  12.                         is dist)  
  13.   --excludes (-e)       comma-separated list of modules to exclude  
  14.   --dll-excludes        comma-separated list of DLLs to exclude  
  15.   --ignores             comma-separated list of modules to ignore if they are  
  16.                         not found  
  17.   --includes (-i)       comma-separated list of modules to include  
  18.   --packages (-p)       comma-separated list of packages to include  
  19.   --compressed (-c)     create a compressed zipfile  
  20.   --xref (-x)           create and show a module cross reference  
  21.   --bundle-files (-b)   bundle dlls in the zipfile or the exe. Valid levels  
  22.                         are 1, 2, or 3 (default)  
  23.   --skip-archive        do not place Python bytecode files in an archive, put  
  24.                         them directly in the file system  
  25.   --ascii (-a)          do not automatically include encodings and codecs  
  26.   --custom-boot-script  Python file that will be run when setting up the  
  27.                         runtime environment  
  28.   
  29. usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]  
  30.    or: setup.py --help [cmd1 cmd2 ...]  
  31.    or: setup.py --help-commands  
  32.    or: setup.py cmd --help  
2: 一个详细的编译脚本
  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": 1     # 所有文件打包成一个zipfile或exe文件,有效级别1,2,3  
  13.             }}  
  14. setup(  
  15.     options=options,               # 是否需要可选项,默认为None  
  16.     zipfile=None,                  # 是否需要压缩像,默认为None  
  17.     console=[{"script": "HelloCmd.py", "icon_resources": [(1, "pc.ico")]}], # 针对CMD控制端口   
  18.     windows=[{"script": "HelloWin.py", "icon_resources": [(1, "pc.ico")]}], # 针对GUI图形窗口  
  19.     data_files=[("magic",["App_x86.exe",]),],  
  20.     version = "v1.01",             # 版本信息  
  21.     description = "py2exe testing",# 描述信息   
  22.     name = "Hello, Py2exe",        # 名字信息  
  23. )  
详情,请参考官方文档:

http://www.py2exe.org/

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Python 安装setuptools方法
python调用wps(在wps中使用python脚本)
Python中setup.py的目的和用途
python、pip、whl安装和使用
Python使用wxPython、py2exe编写桌面程序
Python模块常用的几种安装方式
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服