采用pyinstaller 2.0打包python程序

py2exe最高好像只支持到2.6,pyinstaller可以解决2.7及以上

0:安装python环境包和pywin32;
1:下载pyinstaller,解压缩(201403,最新版本为2.1)
2:setup.py install 进行安装;
3:pyinstaller.py -F xxx.py;
4:会生成xxx目录,进入dist目录找到生成的exe文件。



1使用PyInstaller2将Python脚本转化为可执行文件(上-安装部分)

最近使用Python为项目开发一款测试工具。因为使用者在另一个部门,领导希望能把Python脚本发布为脱离Python平台运行的可执行程序,最好是单个的exe文件,同时也保护我们部门的源码。PyInstaller恰满足这个需求。目前PyInstaller的最新版本是2.0,支持Python2.7。本文将讨论怎样安装PyInstaller2并使其正常工作。文中提到的所有工具都能下载到。本文博客园balian原创,欢迎转载,转载请说明原作者。

下面的截图来自PyInstaller官方网站PyInstaller.org。本文来自博客园balian。笔者使用的是Windows 7平台,下载zip版本的PyInstaller,下载后文件名为pyinstaller-2.0.zip,将这个文件解压缩到需要的文件夹就可以执行,不需要安装。PyInstaller并不区分操作系统是32位还是64位,对Python是否为32位还是64位亦无要求。

image

安装PyInstaller前,需要这样一些准备:安装Python,安装Python Win32 Extensions,以及最好能有压缩工具UPX。

关于Python,参见下图,红色箭头所示为32位版本,下载文件名是python-2.7.3.msi。蓝色箭头所示为64位版本,下载后文件名是python-2.7.3.amd64.msi。显然,如果你是32位Windows XP,你只能安装python-2.7.3.msi。如果你是64位Windows 7,则可以根据需要选择两者之一。Python的安装略过。

image

注意,Python安装完成以后,需要将Python的安装目录加入到系统的环境变量中。如下图。

image

关于Python Win32 Extensions,最新版本是Build 2.18。官网地址http://starship.python.net/~skippy/win32/Downloads.html。下图红色箭头所示是32位版本。显然,如果选择了运行于64位windows的Python,也请使用对应版本的PyWin。Pywin的安装略过。

image

接着就是解压缩PyInstaller。比如,将文件解压到c:\pyinstaller2。

关于UPX,最新版本UPX 3.08,见下图,官网地址http://upx.sourceforge.net/。只有红色箭头所示一个文件下载,文件名是upx308w.zip,并且无所谓32位还是64位操作系统。本文来自博客园balian。请解压这个文件,在解压后的文件夹中找到文件upx.exe,将这个文件直接拷贝到PyInstaller的安装文件夹里。,比如c:\pyinstaller2。

image

综合以上,如果使用PyInstaller打包工具,有32位和64位两种解决方案。

方案一:如果是32/64位Win XP或者32/64位Win 7(本文写作按照该方案。) 
安装python-2.7.3.msi 
安装pywin32-218.win32-py2.7.exe 
解压pyinstaller-2.0.zip 
拷贝upx.exe

方案二:如果是64位Win XP或者64位Win 7 
安装python-2.7.3.amd64.msi 
安装pywin32-218.win-amd64-py2.7.exe 
解压pyinstaller-2.0.zip 
拷贝upx.exe

按照方案一安装结束后,安装文件夹c:\pyinstaller2应如下图。

image

现在就可以从命令行来打包py脚本了。PyInstaller 2.0和以前的版本有一些不同,使用者不需要预先运行Configure.py或者生成spec文件。




2使用PyInstaller2将Python脚本转化为可执行文件(中-使用部分)

main.py是一个简单的Python脚本,在C盘根目录的文件夹scripts中。

'''
main.py
'''
print 'main.py - Hello World!'

 

要将main.py转换为单个exe文件,需要在Pyintstller2安装文件夹下运行如下命令:

python pyinstaller.py -F c:\scripts\main.py

该命令具体执行效果如下图。

image

本文来自博客园balian。单个的main.exe文件可以在文件夹C:\pyinstaller2\main\dist找到,如图。现在main.exe可以用来发布了。

image

命令中参数-F表示生成单个exe文件。如果没有该参数,需要发布的就是一个文件夹了,如下图。

image

PyInstaller2对于Python内部模块的支持如何,使用同样的命令对如下代码打包,没有任何问题。

'''
main.py
'''
import time
print 'main.py - Hello World!'
time.sleep(10)
 

下面看看PyInstaller2对于用户自定义的模块怎么处理。在C盘根目录的文件夹scripts中,有文件test_module.py和main.py。分别如下:

'''
test_module.py
'''
def test_print():
    print 'test_module.py - Import module successfully.!\n'

以及

'''
main.py
'''
import time
import test_module

print 'main.py - Hello World!'
time.sleep(5)
test_module.test_print()
time.sleep(5)

仍旧使用同样的命令打包成单一exe文件,PyInstaller2表示毫无压力。

image



Allowed Options

By default, pyinstaller.py creates a distribution directory containing the main executable and the dynamic libraries. The option --onefile specifies that you want PyInstaller to build a single file with everything inside.

The syntax to use pyinstaller.py is the following:

python pyinstaller.py [opts] <scriptname> [ <scriptname> ...] | <specfile>

Allowed OPTIONS are:

-h--helpshow this help message and exit
-v--versionshow program version
--upx-dir=UPX_DIR
 Directory containing UPX.
--buildpath=BUILDPATH
 Buildpath (default: SPECPATH/build/pyi.TARGET_PLATFORM/SPECNAME)
-y--noconfirm
 Remove output directory (default: SPECPATH/dist/SPECNAME) without confirmation
--log-level=LOGLEVEL
 Log level (default: INFO, choose one of DEBUG, INFO, WARN, ERROR, CRITICAL)

What to generate:

-F--onefilecreate a single file deployment
-D--onedircreate a single directory deployment (default)
-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.
-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.

What to bundle, where to search:

-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.
--additional-hooks-dir=HOOKSPATH
 Additional path to search for hooks. This will go into the extend the hookspath, see Analysis below. This option may be given several times.
-a--asciido NOT include unicode encodings (default: included if available)

How to generate:

-d--debuguse the debug (verbose) build of the executable
-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--noupxdo not use UPX even if available (works differently between Windows and *nix)

Windows and Mac OS X specific options:

-c--console--nowindowed
 use a console subsystem executable (default)
-w--windowed--noconsole
 use a windowed subsystem executable, which on Windows does not open the console when the program is launched. This option is mandatory when creating .app bundle on Mac OS X.
-i FILE.ICO, -i FILE.EXE,ID, -i FILE.ICNS, --icon=FILE.ICO, --icon=FILE.EXE,ID, --icon=FILE.ICNS
If FILE is an .ico file, add the icon to the final executable. Otherwise, the syntax 'file.exe,id' to extract the icon with the specified id from file.exe and add it to the final executable. If FILE is an .icns file, add the icon to the final .app bundle on Mac OS X (for Mac not yet implemented)

Windows specific options:

--version-file=FILE
 add a version resource from FILE to the exe
-m FILE-m XML--manifest=FILE--manifest=XML
 add manifest FILE or XML to the exe
-r FILE[,TYPE[,NAME[,LANGUAGE]]], --resource=FILE[,TYPE[,NAME[,LANGUAGE]]]
add/update resource of the given type, name and language from FILE to the final executable. FILE can be a data file or an exe/dll. For data files, atleast TYPE and NAME need to be specified, LANGUAGE defaults to 0 or may be specified as wildcard * to update all resources of the given TYPE and NAME. For exe/dll files, all resources from FILE will be added/updated to the final executable if TYPE, NAME and LANGUAGE are omitted or specified as wildcard *.Multiple resources are allowed, using this option multiple times.


转载地址:http://www.cnblogs.com/balian/archive/2012/11/21/2780503.html

http://www.cnblogs.com/AnXiaoXi/archive/2012/11/29/2794059.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值