使用PyInstaller——Python程序转换为EXE

来源:互联网 发布:阿里在线编程测验作弊 编辑:程序博客网 时间:2024/06/05 17:13

PyInstaller可以将Python程序打包成Windows(当然也包括Linux, Mac OS X, Solaris and AIX)下可执行的EXE文件,目前支持python2.2-2.7版本,点击这里下载。

使用PyInstaller需要安装PyWin32,可到这里下载相应的版本。(从pywin32的下载量看,还是Python2.7使用更广泛)

 

下载对应已安装的Python版本的PyInstaller版本,解压到任意目录,按照提供的manual文档进行即可。假设要转换的Python代码为txexe\hello.py.进入PyInstaller安装根目录,执行以下命令。

?
python Configure.py #仅第一次执行
python Makespec.py toexe\hello.py #生成的hello.spec文件存默认放在当前目录的hello文件夹下
python Build.py hello\hello.spec #生成的exe文件在当前目录下的hello\dist文件夹下

我们也可以指定Python程序路径以及输出文件路径。

更详细的解释如下:

第一步:Configuring your PyInstaller setup

pyinstaller安装目录下cmd运行:python Configure.py

基于当前系统配置PyInstaller,如果更换了Python版本,需要重新下载相应版本的PyInstaller并重新执行Configure.py

 注意关注运行过程中的警告和错误,最好没有警告和错误,如果出现找不到某dll,可以下载后放到C:\Windows\system32下,一般都能解决。

第二步:Create a spec file for your project

运行:python Makespec.py hello.py

生成要转换的脚本的spec文件,告诉PyInstaller创建一个包含主要的可执行文件和动态库。可以通过-o选项指定输出spec文件的路径:

Makespec.py的可用参数:

-F--onefileproduce a single file deployment (see below).-D--onedirproduce a single directory deployment (default).-K--tkinclude TCL/TK in the deployment.-a--asciido not include encodings. The default (on Python versions with unicode support) is now to include all encodings.-d--debuguse debug (verbose) versions of the executables.-w--windowed--noconsole Use the Windows subsystem executable, which does not open the console when the program is launched. (Windows only)-c--nowindowed--console Use the console subsystem executable. This is the default. (Windows only)-s--stripthe executable and all shared libraries will be run through strip. Note that cygwin's strip tends to render normal Win32 dlls unusable.-X--upxif you have UPX installed (detected by Configure), this will use it to compress your executable (and, on Windows, your dlls). See note below.-o DIR--out=DIR create the spec file in directory. If not specified, and the current directory is Installer's root directory, an output subdirectory will be created. Otherwise the current directory is used.-p DIR--paths=DIR set base path for import (like using PYTHONPATH). Multiple directories are allowed, separating them with the path separator (';' under Windows, ':' under Linux), or using this option multiple times.--icon=<FILE.ICO> add file.ico to the executable's resources. (Windows only)--icon=<FILE.EXE,N> add the n-th incon in file.exe to the executable's resources. (Windows only)-v FILE--version=FILE add verfile as a version resource to the executable. (Windows only)-n NAME--name=NAME optional name to assign to the project (from which the spec file name is generated). If omitted, the basename of the (first) script is used.

常用参数:

-F    制作独立的可执行程序
-D    制作出的档案存放在同一个文件夹下(默认值)

-K    包含TCL/TK(对于使用了TK的,最好加上这个选项,否则在未安装TK的电脑上无法运行)
-w     制作窗口程序
-c    制作命令行程序(默认)
-X    制作使用UPX压缩过的可执行程序(推荐使用这个选项,需要下载UPX包,解压后upx.exe放在Python(非PyInstaller)安装目录下,下载upx308w.zip)

-o DIR  指定输出SPEC文件路径(这也决定了最后输出的exe文件路径)
--icon=[ICO文件路径] 指定程序图标
-v [指定文件] 指定程序版本信息
-n [指定程序名] 指定程序名称

关于SPEC文件细节,点这里。


第三步:Build your project

运行:python Build.py hello\hello.spec

使用上一步得到的spec文件Build,产生的文件放在spec当前目录,有两个文件夹,build是该步骤建立的工程文件,dist存放最后输出的exe文件:hello.exe.

 这里还有一个关于PyInstaller的介绍,很不错:http://bytes.com/topic/python/insights/579554-simple-guide-using-pyinstaller

【完】

原创粉丝点击