Python脚本文件打包成可执行文件的方法


Posted in Python onJune 02, 2015

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

一则: 不需要依赖Python编译器就可以运行软件
二则: 不想让自己的源码公布出去

常用的工具有: py2exe、cx_freeze等

【工具:py2exe】

安装py2exe
安装该工具很简单:
只需要从官方网站:http://www.py2exe.org/下载与版本对应的安装程序,点击下一步即可完成安装。
安装后,执行import py2exe,不报错则表示安装成功!

>>> import py2exe  

>>> 

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

使用py2exe

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

Python脚本文件打包成可执行文件的方法

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

from distutils.core import setup  

import py2exe  

  

setup(windows=['Hello.py']) 

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

D:\temp>setup.py py2exe

Python脚本文件打包成可执行文件的方法

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

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

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

Python脚本文件打包成可执行文件的方法

其他

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

Global options:  

  --verbose (-v)  run verbosely (default)  

  --quiet (-q)    run quietly (turns verbosity off)  

  --dry-run (-n)  don't actually do anything  

  --help (-h)     show detailed help message  

  --no-user-cfg   ignore pydistutils.cfg in your home directory  

  

Options for 'py2exe' command:  

  --optimize (-O)       optimization level: -O1 for "python -O", -O2 for  

                        "python -OO", and -O0 to disable [default: -O0]  

  --dist-dir (-d)       directory to put final built distributions in (default  

                        is dist)  

  --excludes (-e)       comma-separated list of modules to exclude  

  --dll-excludes        comma-separated list of DLLs to exclude  

  --ignores             comma-separated list of modules to ignore if they are  

                        not found  

  --includes (-i)       comma-separated list of modules to include  

  --packages (-p)       comma-separated list of packages to include  

  --compressed (-c)     create a compressed zipfile  

  --xref (-x)           create and show a module cross reference  

  --bundle-files (-b)   bundle dlls in the zipfile or the exe. Valid levels  

                        are 1, 2, or 3 (default)  

  --skip-archive        do not place Python bytecode files in an archive, put  

                        them directly in the file system  

  --ascii (-a)          do not automatically include encodings and codecs  

  --custom-boot-script  Python file that will be run when setting up the  

                        runtime environment  

  

usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]  

   or: setup.py --help [cmd1 cmd2 ...]  

   or: setup.py --help-commands  

   or: setup.py cmd --help 

2: 一个详细的编译脚本

# -*- coding: cp936 -*-  

from distutils.core import setup  

import py2exe  

  

includes = ["encodings", "encodings.*"]  

  

options = {"py2exe":    

            {"compressed": 1,      # 压缩    

             "optimize": 2,        # 优化级别  

             "ascii": 1,           #   

             "includes":includes,  # 编码方式  

             "bundle_files": 1     # 所有文件打包成一个zipfile或exe文件,有效级别1,2,3  

            }}  

setup(  

    options=options,               # 是否需要可选项,默认为None  

    zipfile=None,                  # 是否需要压缩像,默认为None  

    console=[{"script": "HelloCmd.py", "icon_resources": [(1, "pc.ico")]}], # 针对CMD控制端口   

    windows=[{"script": "HelloWin.py", "icon_resources": [(1, "pc.ico")]}], # 针对GUI图形窗口  

    data_files=[("magic",["App_x86.exe",]),],  

    version = "v1.01",             # 版本信息  

    description = "py2exe testing",# 描述信息   

    name = "Hello, Py2exe",        # 名字信息  

) 
Python 相关文章推荐
Python程序员开发中常犯的10个错误
Jul 07 Python
Python中的MongoDB基本操作:连接、查询实例
Feb 13 Python
详解Python中映射类型(字典)操作符的概念和使用
Aug 19 Python
浅谈pandas中Dataframe的查询方法([], loc, iloc, at, iat, ix)
Apr 10 Python
Python Selenium Cookie 绕过验证码实现登录示例代码
Apr 10 Python
python 2.7 检测一个网页是否能正常访问的方法
Dec 26 Python
python 将大文件切分为多个小文件的实例
Jan 14 Python
pyinstaller参数介绍以及总结详解
Jul 12 Python
Django框架表单操作实例分析
Nov 04 Python
判断Threading.start新线程是否执行完毕的实例
May 02 Python
详解Flask前后端分离项目案例
Jul 24 Python
详解Selenium-webdriver绕开反爬虫机制的4种方法
Oct 28 Python
python统计cpu利用率的方法
Jun 02 #Python
Python2.x中文乱码问题解决方法
Jun 02 #Python
python实现的守护进程(Daemon)用法实例
Jun 02 #Python
Python中使用ElementTree解析XML示例
Jun 02 #Python
Python文档生成工具pydoc使用介绍
Jun 02 #Python
自己使用总结Python程序代码片段
Jun 02 #Python
python执行子进程实现进程间通信的方法
Jun 02 #Python
You might like
提高PHP编程效率的53个要点(经验小结)
2010/09/04 PHP
php生成rss类用法实例
2015/04/14 PHP
thinkPHP利用ajax异步上传图片并显示、删除的示例
2018/09/26 PHP
用javascript实现给图片加链接
2007/08/15 Javascript
创建公共调用 jQuery Ajax 带返回值
2012/08/01 Javascript
弹出窗口并且此窗口带有半透明的遮罩层效果
2014/03/13 Javascript
用svg制作富有动态的tooltip
2015/07/17 Javascript
js实现获取当前时间是本月第几周的方法
2015/08/11 Javascript
js为什么不能正确处理小数运算?
2015/12/29 Javascript
JavaScript事件方法(实例讲解)
2017/06/27 Javascript
angular6.x中ngTemplateOutlet指令的使用示例
2018/08/09 Javascript
一百行JS代码实现一个校验工具
2019/04/30 Javascript
使用vue for时为什么要key【推荐】
2019/07/11 Javascript
webpack的pitching loader详解
2019/09/23 Javascript
微信小程序实现点击按钮后修改颜色
2019/12/05 Javascript
Python学习思维导图(必看篇)
2017/06/26 Python
Python中函数及默认参数的定义与调用操作实例分析
2017/07/25 Python
Python数据可视化正态分布简单分析及实现代码
2017/12/04 Python
在Python程序员面试中被问的最多的10道题
2017/12/05 Python
Windows下安装Scrapy
2018/10/17 Python
python3实现网络爬虫之BeautifulSoup使用详解
2018/12/19 Python
django admin后台添加导出excel功能示例代码
2019/05/15 Python
深入了解如何基于Python读写Kafka
2019/12/31 Python
详解HTML5常用的语义化标签
2019/09/27 HTML / CSS
英国最大的LED专业零售商:Led Hut
2018/03/16 全球购物
俄罗斯在线水暖商店:Perfecto.ru
2019/10/25 全球购物
新闻记者个人求职的自我评价
2013/11/28 职场文书
中专生自我鉴定范文
2013/12/19 职场文书
酒店总经理欢迎词
2014/01/08 职场文书
聚美优品的广告词
2014/03/14 职场文书
2015年医德考评自我评价
2015/03/03 职场文书
2015年爱国卫生工作总结
2015/04/22 职场文书
关于上班时间调整的通知
2015/04/23 职场文书
初中思想品德教学反思
2016/02/24 职场文书
MySQL常见优化方案汇总
2022/01/18 MySQL
使用MybatisPlus打印sql语句
2022/04/22 SQL Server