您的位置:首页 > 编程语言 > Python开发

将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. 执行结果
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: