用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
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # Created By MagicTong 2010-11-01 13:01
- def main():
- print "Hello, Py2exe! "
- if __name__ == "__main__":
- main()
- raw_input("Enter enter key to exit...")
然后写一个编译脚本,文件名:setup_py2exe.py
- from distutils.core import setup
- import py2exe
- setup(console=["HelloPy2exe.py"])
最后整个批处理,文件名:buildExe.bat
- python setup_py2exe.py py2exe
- 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 可以打印出来,但是这些参数我也没用过,有空再摸索摸索)
- 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
- 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_py2exe.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
- or: setup_py2exe.py --help [cmd1 cmd2 ...]
- or: setup_py2exe.py --help-commands
- or: setup_py2exe.py cmd --help
六、高级,其实也不高级
看那个编译脚本中的这句:setup(console=["HelloPy2exe.py"]),setup还支持很多参数,windows(一个windows界面程序),data_filse(打包其他的文件)……以后再说说。
看一个例子先:
- # -*- 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 #所有文件打包成一个exe文件
- }}
- setup(
- options=options,
- zipfile=None,
- console=[{"script": "HelloPy2exe.py", "icon_resources": [(1, "pc.ico")]}],
- windows=[{"script": "HelloWin.py", "icon_resources": [(1, "pc.ico")]}],
- data_files=[("magic",["App_x86.exe",]),],
- version = "2010.11.01.01",
- description = "this is a py2exe test",
- name = "HelloGuys.",
- )
options可以用来指定一些编译的参数,譬如是否压缩,是否打包为一个文件,data_files是一个打包时的拷贝文件列表,格式如下:data_files=[("目的文件夹",["文件名",]), ("目的文件夹",["文件名",]), ("目的文件夹",["文件名",]),],至于version,description,name等等,你们懂的,icon_resources是指定一个ico图标作为程序的图标。从这里也可以看出windows,console等参数是可以指定一个list来设置参数的。可以去Python安装目录/Lib/site-packages/py2exe/samples下看一些例子,这玩意还可以打包服务程序,com服务器程序等等。
[end]
用Py2exe打包Python脚本简单介绍的更多相关文章
- py2exe打包python脚本
在工作中遇到将python脚本转换成exe可执行程序的需求,通过查询可以使用py2exe来构建满足要求的程序,这里简要说明一下使用步骤. 一.py2exe是一个将python脚本转换成windows上 ...
- PyInstaller打包python脚本的一些心得
PyInstaller打包python脚本的一些心得 因为在公司经常要帮同事做一个从excel表格中提取出需要的内容的重复工作,比较繁琐还容易出错:于是就想着要写个程序,但是同事又不可能在电脑上也装上 ...
- 打包python脚本为exe可执行文件-pyinstaller和cx_freeze示例
本文介绍使用cx_freeze和pyinstaller打包python脚本为exe文件 cx_freeze的使用实例 需要使用到的文件wxapp.py, read_file.py, setup.py ...
- 打包python脚本为exe的坎坷经历, by pyinstaller方法
打包python脚本为exe的坎坷经历, by pyinstaller方法 又应验了那句歌词. 不经历风雨, 怎么见得了彩虹. 安装过程略去不提, 仅提示: pip install pyinstall ...
- Python redis 简单介绍
Python redis 简单介绍 1.安装 终端输入: pip(or)pip3.6 install redis 安装成功 2.哈哈,发现我并没有redis服务可以访问,所以到这里,在本机安装了red ...
- 使用 py2exe 打包 Python 程序
上回在<使用 PyInstaller 打包 Python 程序>中,我们介绍了使用 PyInstaller 对 Python 程序进行打包,今天带大家认识一个新的工具:py2exe. 接下 ...
- 使用py2exe将python脚本转换成exe可执行文件
Python(wiki en chs)是一门弱类型解释型脚本语言,拥有动态类型系统和垃圾回收功能,支持多种编程范式:面向对象.命令式.函数式和过程式编程. 由于Python拥有一个巨大而广泛的标准库 ...
- Python的简单介绍
0. 前言 最近在从头梳理Python的相关知识,有助于以后更好地学习新知识.这篇博客,我简单介绍一下Python语言的有关内容. 1. Python介绍 Python的创始人为荷兰人吉多·范罗苏姆( ...
- Python学习笔记:py2exe打包Python程序
使用py2exe将一个Python程序打包成一个exe程序,这样Python程序也可以在没有安装Python的环境中运行Python程序了.使用这个工具需要写一个用于打包的setup.py文件(名称可 ...
随机推荐
- textview设置不同字体大小
<style name="style0"> <item name="android:textSize">19dip</item&g ...
- react总结
在我的工作用到的最多就是backbone,其次还会有ember/Ext,backbone目前能实现我们team所需要实现的功能,因为我们的component不需要频繁的操作Dom,当后台API返回数据 ...
- 在cmd中连接数据库
1.进入mysql安装路径的 mysql/bin 目录(即mysqld.exe所在目录) 2.在cmd中输入mysql进入mysql操作环境(mysql -u root -p )链接mysql并输入密 ...
- ftp服务器端的安装及配置
搭建过程 安装 vsftp 服务(yum 安装即可) 配置/etc/vsftpd/vsftpd.conf anonymous_enable=NO #禁止匿名登录 local_enable=YES ...
- Android:自定义Dialog大小,显示圆角
经过测试,可以使用. ----------------------------------------------------------- AlertDialog.Builder builder = ...
- open()的模块
对文件操作流程: 1.打开文件,得到文件句柄并赋值给一个变量 2.通过句柄对文件进行操作 3.关闭文件 mode can be: * 'r' 只读. * 'w' 写入,如果之前有就覆盖 * 'a' ...
- npm 使用代理
npm install 有时候会安装失败,可能是网络的问题,可以使用代理来安装 npm获取配置有6种方式,优先级由高到底. 命令行参数. --proxy http://server:port即将pro ...
- php 导出 Excel 报错 exception 'PHPExcel_Calculation_Exception' with message
exception 'PHPExcel_Calculation_Exception' with message '粉丝数据!C2679 -> Formula Error: Operator '= ...
- apt-get 安装路径
apt-get安装目录和安装路径:apt-get 下载后,软件所在路径是:/var/cache/apt/archivesubuntu 默认的PATH为PATH=/home/brightman/bin: ...
- 【LeetCode】459. Repeated Substring Pattern
Given a non-empty string check if it can be constructed by taking a substring of it and appending mu ...