当前位置: 移动技术网 > IT编程>脚本编程>Python > 使用PyInstaller将python转成可执行文件exe笔记

使用PyInstaller将python转成可执行文件exe笔记

2018年08月21日  | 移动技术网IT编程  | 我要评论

600影视,喷泉模型下载,莆田二手房

1、安装pyinstaller

pyinstaller的作用如标题所说,首先需要下载pyinstaller和upx,upx是用来压缩exe的,点击超链接下载吧,目前稳定版本是1.3,注意选择你使用的操作系统。如在windows下,将下载解压后的upx.exe放到pyinstaller解压后的文件夹内。设pyinstaller的文件夹为d:\pyinstaller,下同。命令行下进入d:\pyinstaller,运行configure.py,应该看到如下信息:

i: computing exe_dependencies
i: finding tcl/tk...
i: found tcl/tk version 8.5
i: testing for zlib...
i: ... zlib available
i: testing for ability to set icons, version resources...
i: ... resource update available
i: testing for unicode support...
i: ... unicode available
i: testing for upx...
i: ...upx available
i: computing pyz dependencies...

不能出现开头为e(error)的信息,最好不要有w(warning)的信息。如果出现找不到某dll,请把该dll文件放置到c:\windows\system32下,一般能解决。

linux用户还需要编译runtime executables,windows用户不需要。主要是运行make.py,会在pyinstaller\support\load\下生成run和run_d两个文件,详见pyinstaller\doc\manual.html说明。

2、写一个py程序

为了实验,写一个helloworld.py,假设保存在c:\helloworld.py

#!/usr/bin/env python
print 'hello,world!'
words = raw_input('what do you want to say? ')
print 'you said:'+words

3、创建spec文件

spec文件是用来告诉pyinstaller要编译的py文件和参数的。执行"makespec.py+参数+py代码路径"就可以,主要参数如下(详见pyinstaller\doc\manual.html):

-f, --onefile py代码只有一个文件
-d, --onedir py代码放在一个目录中(默认是这个)
-k, --tk 包含tcl/tk
-d, --debug 生成debug模式的exe文件
-w, --windowed, --noconsole 窗体exe文件(windows only)
-c, --nowindowed, --console 控制台exe文件(windows only)
-x, --upx 使用upx压缩exe文件
-o dir, --out=dir 设置spec文件输出的目录,默认在pyinstaller同目录
--icon=<file.ico> 加入图标(windows only)
-v file, --version=file 加入版本信息文件

对于helloworld.py,具体执行以下代码:

makespec.py --onefile --console --upx --tk -o c:\ c:\helloworld.py

执行后c:\就出现helloworld.spec

4、build spec文件,生成exe文件

执行:

build.py c:\helloworld.spec  

一长串信息之后,你会在c:\下发现helloworld.exe这个文件,就是它啦!体积还真不小,2.7m,这是因为这个helloworld程序杀鸡用牛刀罢了。此外其它的文件均是过程文件,可删除。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网