使用py2exe生成独立的exe文件
2014-07-24 18:57
246 查看
今天写一个python小脚本,在windows下将当前状态下,所有的task的名字输出到一个文件里,然后将这个脚本转化成exe文件。
先看一下python脚本TaskNameList.py:
Python代码
import subprocess
# running the command "tasklist" in cmd.exe
popen = subprocess.Popen("tasklist",stdout=subprocess.PIPE,shell= True)
namelist = []
# get the task name
for line in popen.stdout.readlines()[3:]:
name = line.split()[0]
if name not in namelist:
namelist.append(name)
popen.stdout.close()
# wirte the name into the file TaskNameList
file = open(r".\TaskNameList.txt",'w')
file.writelines('\n'.join(namelist))
file.close()
接下来我用了第三方的软件py2exe,将python脚本软化为exe可执行文件,在看完了py2exe官网上的tutorial后,写一个setup.py脚本
Python代码
from distutils.core import setup
import py2exe
setup(console=["'TaskNameList.py"])
执行python setup.py py2exe后,会生成两个文件夹dist 和build,build文件对我们没有多大用处,而在dist文件夹下,会有一个TaskNameList.exe可执行文件,这就我们要的exe文件,但是,运行这个文件的提前是不能脱离dist这个文件夹,在这个文件夹下的其它文件都是在exe执行时会调用到的,所以要想移动这个文件到,要连同dist这个文件夹一起移动,否则,单独移动exe文件,它是无法正常执行的。所以 最好的能生成一个将exe所调用的文件和其本身都绑定到一起的exe文件。
在查阅了资料后,我重写了一setup.py方法:
Python代码
from distutils.core import setup
import py2exe
import sys
includes = ["encodings", "encodings.*"]
sys.argv.append("py2exe")
options = {"py2exe": { "bundle_files": 1 }
}
setup(options = options,
zipfile=None,
console = [{"script":'TaskNameList.py'}])
这次直接执行python setup.py,就可以生成一个独立的exe文件了,当然这个文件还是在dist文件夹下。
这个文件比之前那个的最重要的改进在于两个参数:
Python代码
"bundle_files": 1
我们可以看看官网给出的有效的bundle_files的值:
Python代码
zipfile=None,
zipfile = None指定把library.zip也打包进exe 里了。
附:
给生成的exe文件加上图标:
Python代码
from distutils.core import setup
import py2exe
import sys
includes = ["encodings", "encodings.*"]
sys.argv.append("py2exe")
options = {"py2exe": { "bundle_files": 1 }
}
setup(options = options,
zipfile=None,
console = [{"script":'TaskNameList.py',
'icon_resources':[(1, 'logo.ico')]}])
先看一下python脚本TaskNameList.py:
Python代码
import subprocess
# running the command "tasklist" in cmd.exe
popen = subprocess.Popen("tasklist",stdout=subprocess.PIPE,shell= True)
namelist = []
# get the task name
for line in popen.stdout.readlines()[3:]:
name = line.split()[0]
if name not in namelist:
namelist.append(name)
popen.stdout.close()
# wirte the name into the file TaskNameList
file = open(r".\TaskNameList.txt",'w')
file.writelines('\n'.join(namelist))
file.close()
接下来我用了第三方的软件py2exe,将python脚本软化为exe可执行文件,在看完了py2exe官网上的tutorial后,写一个setup.py脚本
Python代码
from distutils.core import setup
import py2exe
setup(console=["'TaskNameList.py"])
执行python setup.py py2exe后,会生成两个文件夹dist 和build,build文件对我们没有多大用处,而在dist文件夹下,会有一个TaskNameList.exe可执行文件,这就我们要的exe文件,但是,运行这个文件的提前是不能脱离dist这个文件夹,在这个文件夹下的其它文件都是在exe执行时会调用到的,所以要想移动这个文件到,要连同dist这个文件夹一起移动,否则,单独移动exe文件,它是无法正常执行的。所以 最好的能生成一个将exe所调用的文件和其本身都绑定到一起的exe文件。
在查阅了资料后,我重写了一setup.py方法:
Python代码
from distutils.core import setup
import py2exe
import sys
includes = ["encodings", "encodings.*"]
sys.argv.append("py2exe")
options = {"py2exe": { "bundle_files": 1 }
}
setup(options = options,
zipfile=None,
console = [{"script":'TaskNameList.py'}])
这次直接执行python setup.py,就可以生成一个独立的exe文件了,当然这个文件还是在dist文件夹下。
这个文件比之前那个的最重要的改进在于两个参数:
Python代码
"bundle_files": 1
我们可以看看官网给出的有效的bundle_files的值:
3 (default) | don't bundle |
2 | bundle everything but the Python interpreter |
1 | bundle everything, including the Python interpreter |
zipfile=None,
zipfile = None指定把library.zip也打包进exe 里了。
附:
给生成的exe文件加上图标:
Python代码
from distutils.core import setup
import py2exe
import sys
includes = ["encodings", "encodings.*"]
sys.argv.append("py2exe")
options = {"py2exe": { "bundle_files": 1 }
}
setup(options = options,
zipfile=None,
console = [{"script":'TaskNameList.py',
'icon_resources':[(1, 'logo.ico')]}])
相关文章推荐
- 使用py2exe生成独立的exe文件
- 使用py2exe将python 脚本生成exe可执行文件
- python 使用py2exe将python 脚本生成exe可执行文件
- 使用py2exe对Python文件进行封装生成exe文件
- windows7下使用py2exe把python打包程序为exe文件
- 如何使用RC.exe生成VB中的RES文件
- htpasswd.exe怎样使用 (svn用户名密码生成文件)
- ADO.NET Entity Framework 如何:使用 EdmGen.exe 生成模型和映射文件
- 使用VS进行打包程序解决生成两个文件的问题(压缩后只有一个exe)
- 把Python脚本打包成exe文件 ——py2exe使用小记
- VS2010下无法找到“XXX.exe”的调试信息,或者调试信息不匹配。未使用调试信息生成二进制文件。
- 错误:无法找到“XXX.exe”的调试信息,或者调试信息不匹配。未使用调试信息生成二进制文件。
- 使用了Py2exe编译单个exe文件
- matlab将M文件直接编译为可独立使用的EXE可执行文件
- 2013-12-25VS2008“无法找到“xxx.exe”的调试信息,或者调试信息不匹配。未使用调试信息生成二进制文件。”
- 无法找到“XXX.exe”的调试信息,或者调试信息不匹配。未使用调试信息生成二进制文件
- 使用SVCUtil.exe生成客户端代理类和配置文件
- 使用SVCUtil.exe生成客户端代理类和配置文件
- 编译,出现“无法找到“***.exe”的调试信息,或者调试信息不匹配。未使用调试信息生成二进制文件。”
- 无法找到“XXX.exe”的调试信息,或者调试信息不匹配。未使用调试信息生成二进制文件。”遇到这种情况肿么办???