将Python3.2脚本封装成exe可执行文件
2017-03-08 09:50
411 查看
cx_freeze是用来将 Python 脚本封装成可执行程序的工具,支持最新的Python3.2版本。生成的执行文件具有跨平台性,而且运行的系统无须安装Python。目前类似功能的工具还有py2exe 和
PyInstaller,其中貌似py2exe知名度最高了,但是很久没有更新了,至于打包质量不做评价,毕竟萝卜青菜各有所爱;PyInstaller不太了解,据说工序很复杂;至于cx_freeze的强大功能及易用性,本人强烈推荐。
详细安装步骤如下:
1. 安装cx_freeze(官方下载地址:http://cx-freeze.sourceforge.net)
2. 检查cx_freeze安装是否成功(Windows OS)
3. 准备一个简单的随机画笑脸RandomSmileys.py小程序,放在C:\Python32\Scripts
#RandomSmileys.py
import random
import turtle
t = turtle.Pen()
t.speed(0)
t.hideturtle()
turtle.bgcolor("black")
def draw_smiley(x,y):
t.penup()
t.setpos(x,y)
t.pendown
#head
t.pencolor("yellow")
t.fillcolor("yellow")
t.begin_fill()
t.circle(50)
t.end_fill()
#left eye
t.pencolor("blue")
t.setpos(x-15,y+60)
t.fillcolor("blue")
t.begin_fill()
t.circle(10)
t.end_fill()
#right eye
t.setpos(x+15,y+60)
t.begin_fill()
t.circle(10)
t.end_fill()
#mouth
t.setpos(x-25,y+40)
t.pendown()
t.pencolor("black")
t.width(10)
t.goto(x-10,y+20)
t.goto(x+10,y+20)
t.goto(x+25,y+40)
t.width(1)
for n in range(50):
x = random.randrange(-turtle.window_width()//2,turtle.window_width()//2)
y = random.randrange(-turtle.window_height()//2,turtle.window_height()//2)
draw_smiley(x,y)
print("####RandomSmileys.py结束####")
4. 把Python的脚本封装成可执行文件(两种方法)
使用参数:
CMD> cxfreeze hello.py --target-dir dist
使用配置文件(个人推荐=>一次编写,到处可用☺):
CMD> python setup.py build
setup.py配置程序:
我在网上找的setup.py程序
setup.py
#
# 文 件 名:setup.py
# 功能描述:cx_freeze封装Python脚本的配置文件
#
# 作者:Renzo 日期:2012/01/01
#
# 版权:可以使用、传播,但请保留出处;如需修改,请告知作者。
#
from cx_Freeze import setup, Executable
# 首先处理path,includes,excludes,packages等内部变量
base = "Win32GUI"
path = []
includes = []
excludes = ['_gtkagg', '_tkagg', 'bsddb', 'curses', 'email', 'pywin.debugger',
'pywin.debugger.dbgcon', 'pywin.dialogs', 'tcl', 'Tkconstants',
'Tkinter']
packages = []
# 这里可以编写客户化的封装前处理代码。例如:数据文件的处理
# 配置封装的参数
GUI2Exe_Target_Main = Executable(
path = path,
base = base,
# 生成可执行文件的主文件
script = "simple.py",
# 生成可执行文件及一些依赖文件的目录
targetDir = r"dist",
# 可执行文件的名称
targetName = "simple.exe",
# 可执行文件的ico图标
icon = "simple.ico",
includes = includes,
excludes = excludes,
packages = packages,
# 是否需要压缩模块的字节码
compress = True,
# 是否拷贝依赖文件到目标目录
copyDependentFiles = True,
# 是否附加脚本模块到执行文件
appendScriptToExe = True,
# 是否添加脚本模块到共享库
appendScriptToLibrary = False,
# 设置快捷方式的路径及名称
shortcutDir = "",
shortcutName = ""
)
# 设置安装时软件包的描述信息
setup(
name = "Simple",
version = "0.1",
description = "My first python program",
author = "Renzo",
author_email = "liurenzhong@yeah.net",
url = "wwww.cnblogs.com/renzo",
# 生成的可执行文件
executables = [GUI2Exe_Target_Main]
)
# 这里可以编写客户化的封装后处理代码。例如:临时数据的清除,数据包的发布等
# 到此,整个setup脚本已经完成。
这个程序是有点问题的,原py程序的import包无法打包,生成了exe文件但是无法运行,但是这个注释写的很详细,有助于理解。
然后又找了一段代码,是正确的
#code
import sys
import os
from cx_Freeze import setup, Executable
build_exe_options = {'packages': [], 'excludes' : []}
base = 'Win32GUI'
exe = Executable(
script = 'RandomSmileys.py',
initScript = None,
base = 'Win32GUI',
targetName = 'MedicaidAid.exe',
compress = True,
appendScriptToExe = True,
appendScriptToLibrary = True,
icon = None
)
setup( name = 'MedicaidAid',
version = '0.85',
description = 'MedicaidAid Software',
options = {'build_exe': build_exe_options},
executables = [Executable('RandomSmileys.py', base = base)])
5. 生成的可执行文件(xxxx.exe)
6. 执行结果
PyInstaller,其中貌似py2exe知名度最高了,但是很久没有更新了,至于打包质量不做评价,毕竟萝卜青菜各有所爱;PyInstaller不太了解,据说工序很复杂;至于cx_freeze的强大功能及易用性,本人强烈推荐。
详细安装步骤如下:
1. 安装cx_freeze(官方下载地址:http://cx-freeze.sourceforge.net)
2. 检查cx_freeze安装是否成功(Windows OS)
3. 准备一个简单的随机画笑脸RandomSmileys.py小程序,放在C:\Python32\Scripts
#RandomSmileys.py
import random
import turtle
t = turtle.Pen()
t.speed(0)
t.hideturtle()
turtle.bgcolor("black")
def draw_smiley(x,y):
t.penup()
t.setpos(x,y)
t.pendown
#head
t.pencolor("yellow")
t.fillcolor("yellow")
t.begin_fill()
t.circle(50)
t.end_fill()
#left eye
t.pencolor("blue")
t.setpos(x-15,y+60)
t.fillcolor("blue")
t.begin_fill()
t.circle(10)
t.end_fill()
#right eye
t.setpos(x+15,y+60)
t.begin_fill()
t.circle(10)
t.end_fill()
#mouth
t.setpos(x-25,y+40)
t.pendown()
t.pencolor("black")
t.width(10)
t.goto(x-10,y+20)
t.goto(x+10,y+20)
t.goto(x+25,y+40)
t.width(1)
for n in range(50):
x = random.randrange(-turtle.window_width()//2,turtle.window_width()//2)
y = random.randrange(-turtle.window_height()//2,turtle.window_height()//2)
draw_smiley(x,y)
print("####RandomSmileys.py结束####")
4. 把Python的脚本封装成可执行文件(两种方法)
使用参数:
CMD> cxfreeze hello.py --target-dir dist
使用配置文件(个人推荐=>一次编写,到处可用☺):
CMD> python setup.py build
setup.py配置程序:
我在网上找的setup.py程序
setup.py
#
# 文 件 名:setup.py
# 功能描述:cx_freeze封装Python脚本的配置文件
#
# 作者:Renzo 日期:2012/01/01
#
# 版权:可以使用、传播,但请保留出处;如需修改,请告知作者。
#
from cx_Freeze import setup, Executable
# 首先处理path,includes,excludes,packages等内部变量
base = "Win32GUI"
path = []
includes = []
excludes = ['_gtkagg', '_tkagg', 'bsddb', 'curses', 'email', 'pywin.debugger',
'pywin.debugger.dbgcon', 'pywin.dialogs', 'tcl', 'Tkconstants',
'Tkinter']
packages = []
# 这里可以编写客户化的封装前处理代码。例如:数据文件的处理
# 配置封装的参数
GUI2Exe_Target_Main = Executable(
path = path,
base = base,
# 生成可执行文件的主文件
script = "simple.py",
# 生成可执行文件及一些依赖文件的目录
targetDir = r"dist",
# 可执行文件的名称
targetName = "simple.exe",
# 可执行文件的ico图标
icon = "simple.ico",
includes = includes,
excludes = excludes,
packages = packages,
# 是否需要压缩模块的字节码
compress = True,
# 是否拷贝依赖文件到目标目录
copyDependentFiles = True,
# 是否附加脚本模块到执行文件
appendScriptToExe = True,
# 是否添加脚本模块到共享库
appendScriptToLibrary = False,
# 设置快捷方式的路径及名称
shortcutDir = "",
shortcutName = ""
)
# 设置安装时软件包的描述信息
setup(
name = "Simple",
version = "0.1",
description = "My first python program",
author = "Renzo",
author_email = "liurenzhong@yeah.net",
url = "wwww.cnblogs.com/renzo",
# 生成的可执行文件
executables = [GUI2Exe_Target_Main]
)
# 这里可以编写客户化的封装后处理代码。例如:临时数据的清除,数据包的发布等
# 到此,整个setup脚本已经完成。
这个程序是有点问题的,原py程序的import包无法打包,生成了exe文件但是无法运行,但是这个注释写的很详细,有助于理解。
然后又找了一段代码,是正确的
#code
import sys
import os
from cx_Freeze import setup, Executable
build_exe_options = {'packages': [], 'excludes' : []}
base = 'Win32GUI'
exe = Executable(
script = 'RandomSmileys.py',
initScript = None,
base = 'Win32GUI',
targetName = 'MedicaidAid.exe',
compress = True,
appendScriptToExe = True,
appendScriptToLibrary = True,
icon = None
)
setup( name = 'MedicaidAid',
version = '0.85',
description = 'MedicaidAid Software',
options = {'build_exe': build_exe_options},
executables = [Executable('RandomSmileys.py', base = base)])
5. 生成的可执行文件(xxxx.exe)
6. 执行结果
相关文章推荐
- Tools_将Python脚本封装成exe可执行文件
- 将Python脚本封装成exe可执行文件
- 将Python脚本封装成exe可执行文件 转
- python脚本生成exe可执行文件
- 将Python脚本打包成可执行文件.exe
- Python脚本如何生成Windows可执行文件.exe
- PyInstaller对python脚本进行代码打包成单个独立的exe可执行文件
- 打包python脚本为exe可执行文件-pyinstaller和cx_freeze示例
- 将Python脚本文件转化成Windows可执行exe文件
- python 使用py2exe将python 脚本生成exe可执行文件
- 用pyinstaller将Pythone 脚本生成EXE可执行文件发布
- 使用py2exe将python 脚本生成exe可执行文件
- python 将py脚本打包生成exe可执行文件
- 将Python脚本转为exe执行文件
- Python脚本如何生成Windows可执行文件.exe
- python中获取打包成执行文件(exe)和脚本运行文件的路径
- python脚本打包exe可执行文件
- 使用pyinstaller将python脚本转换成exe可执行文件
- 如何将python程序封装成exe可执行文件
- Python脚本如何生成Windows可执行文件.exe