现在的位置: 首页 > 综合 > 正文

使用了Py2exe编译单个exe文件

2013年06月25日 ⁄ 综合 ⁄ 共 2360字 ⁄ 字号 评论关闭

们经常要用py2exepython脚本转换为可执行文件,但是有一个麻烦就是要针对脚本写一个setup.py,在setup.py中指定一些信息,然后运行setup.py XX.py来生成执行文件。为避免每次都要写setup.py,以下提供一个脚本工具pyfly.py,只要运行pyfly.py
yourfile.py
就能生成yourfile.exe。还可以为其指定其他参数,比如-i myicon.ico为生成的可执行文件添加图标;-p
console
指定生成控制台程序,-p windows指定生成windows程序;-c yes指定用upx压缩可执行文件以减小大小,-c no执行不压缩可执行文件;-d out_directory 指定生成的可执行文件的输出目录。(-c yes 选项需要将upx.exe放置在系统的环境变量PATH所包含的目录中,或者将upx.exe的路径加入PATH)

运行"pyfly.py pyfly.py", 就立即在当前目录下生成pyfly.exe,pyfly.exe置于环境变量PATH中能找到的位置,然后在任意目录中就可为你的python脚本生成单文件的可执行程序了。

要想运行该段代码,你需要准备以下两件事,下载WConio,该模块主要是用来控制windows控制台彩色文字输出的,下载地址为:-

http://newcenturycomputers.net/projects/wconio.html
,记住下载exe的版本,Python2.5版本的下载地址为:http://newcenturycomputers.net/projects/download.cgi/WConio-1.5.win32-py2.5.exeWinrar打开该exe,将里面的WConio.py_WConio.pyd直接复制到D:/Python25/Lib/site-packages目录下就可以了。下面是WConio官方的例子

# Works with Microsoft Windows dos box

# Shows some use of WConio written by Chris Gonnerman

 

# Written by Priyend Somaroo

# Copyright (c) 2008 Vardaan Enterprises, www.vardaan.com

 

# Use and distribute freely.

# No liability for any use of this code will be accepted. Use is

# without any warranty whatsoever

 

# Requires the package WConio by Chris Gonnerman

# E-Mail : chris.gonnerman@newcenturycomputers.net

# Web : http://newcenturycomputers.net/projects/wconio.html

 

import WConio

 

#Store current attribute settings

old_setting = WConio.gettextinfo()[4] & 0x00FF

 

#Clear the screen

WConio.clrscr()

 

#Display something in low video

WConio.lowvideo()

WConio.cputs("Low video/r/n")

 

#Display something in high video

WConio.highvideo()

WConio.cputs("High video/r/n")

 

#Display something in normal video

WConio.normvideo()

WConio.cputs("Normal video/r/n")

 

#Display some text in color

WConio.textattr(WConio.LIGHTRED)

WConio.cputs("Light Red text/r/n")

 

#Display some more text in color

WConio.textattr(WConio.LIGHTBLUE)

WConio.cputs("Light BLUE text/r/n")

 

#leave a blank line - this shows you that print still works

print

 

#Set heading colour but using print

WConio.textattr(WConio.LIGHTGREEN)

print("Times table/r/n")

 

#Back to normal intensity for white

WConio.normvideo()

 

for i in range(12) :

  WConio.textattr(WConio.WHITE)

  a = "%2d * 2 = " % (i)

  WConio.cputs(a)

  WConio.textattr(WConio.YELLOW)

  a =  "%2d/r/n" % (i*2)

  WConio.cputs(a)

 

WConio.textattr(WConio.CYAN)

WConio.cputs("/n/nPress any key to end/r/n")

 

#Wait for a key to be pressed

WConio.getch()

 

#Retore old attribute settings

WConio.textattr(old_setting)

接下来下载upxhttp://upx.sourceforge.net/

下面是pyfly.py的源代码

#-*- coding: utf8 -*-

import os,sys,re,time,uuid,subprocess,shutil,WConio

 

def

抱歉!评论已关闭.