1.下载py2exe程序
2.安装
3.写一段python test.py
import sys
def test():
print "HelloWorld"
if __name == "__main__":
test()
4.再写一段python调用py2exe的程序,exce.py
from distutils.core import setup
import py2exe
setup(console=['test.py'])
5.在当前路径调用cmd.bat里面写cmd.exe
执行cmd.exe,输入命令python exce.py py2exe
即可在当前目录生成一个dist文件夹内有test.exe和其他依赖组件
6.调用test.exe
7.若有引用复写方法的时候
要这么写
from ConfigParser import ConfigParser
class CaseConfigParser(ConfigParser):
def __init__(self):
ConfigParser.__init__(self)
def optionxform(self, optionstr):
return optionstr
import sys
import CaseConfigParser
def modifyDSNodeIp(filename,ip):
new_Data = ""
DSNodeFile = open(filename,"rb")
DSNode_Data = DSNodeFile.readlines()
for line in DSNode_Data:
line = line.strip()
if line.endswith("ICD_DB_SERVICE"):
new_line = line.replace(line,"@SERVER="+ip+"\ipcc@ICD_DB_SERVICE")
new_Data = new_Data + new_line+"\n"
elif line.endswith("@hpsdb"):
new_line = line.replace(line,"@SERVER="+ip+"\ipcc@hpsdb")
new_Data = new_Data + new_line+"\n"
else:
new_Data = new_Data + line+"\n"
DSNodeFile.close()
DSNodeFile = open(filename,"wb")
DSNodeFile.write(new_Data)
DSNodeFile.close()
def modifyDSParIp(filename,ip):
new_Data = ""
DSPar = open(filename,"rb")
DSPar_Data = DSPar.readlines()
for line in DSPar_Data:
line = line.strip()
if line.startswith("listen_ip"):
new_line = line.replace(line,"listen_ip ="+ip)
new_Data = new_Data + new_line+"\n"
else:
new_Data = new_Data + line+"\n"
DSPar.close()
DSPar = open(filename,"wb")
DSPar.write(new_Data)
DSPar.close()
def modifyIcdcommIp(filename,ip):
cfg = CaseConfigParser.CaseConfigParser()
cfg.read(filename)
cfg.set("common", "ICDTCPMYIP", ip)
cfg.write(open(filename,"wb"))
if __name__ == "__main__":
ip = sys.argv [ 1 ]
modifyDSNodeIp("C:\\Program Files (x86)\\datastation\\bin\\DSNode.txt",ip)
modifyDSParIp("C:\\Program Files (x86)\\datastation\\bin\\DSParameters.txt",ip)
modifyIcdcommIp("C:\\Windows\\SysWOW64\\icdcomm.cfg",ip)
from distutils.core import setup
import py2exe
setup(console=['modifySQLFSScript.py','CaseConfigParser.py'])
要把两个py 写在一起 如果分开写的话 系统找不到另外一个py