1 PyInstaller简介
1.1 PyInstaller特性
Ø 可以将Python脚本打包成可执行程序,在没有Python环境的机器上运行。
Ø 可以在以下环境中执行:
Windows (32-bit and 64-bit),
Linux (32-bit and 64-bit),
Mac OS X (32-bit and 64-bit),
experimentally Solaris and AIX.
Ø 打包环境支持Python2.3-2.7
Ø 支持对第三方程序包的打包支持,支持的程序包如下:
babel |
OK |
Fully supported, including "localedata" external data files (automatically handled). |
chardet |
OK |
|
ctypes |
OK |
Features/CtypesDependencySupport in PyInstaller 1.4+ |
cx_Oracle |
OK |
|
Django |
OK |
Preliminar support, see Recipe/DjangoApplication for howto |
docutils |
OK |
development branch |
ElementTree |
OK |
|
Filelike |
OK |
|
gadfly |
OK |
|
Greenlet |
OK |
|
idlelib |
OK |
development branch |
IPython |
OK |
development branch |
lxml |
OK |
|
Mako |
OK |
|
Matplotlib |
OK |
Fully supported, including external data files (automatically packaged by PyInstaller). |
numpy |
OK |
|
paste |
OK |
|
PIL |
OK |
|
psycopg2 |
OK |
|
pyenchant |
OK |
Windows only for now. |
pyexpat |
OK |
|
PyGame |
OK |
|
pygments |
OK |
|
PyGTK+ |
OK |
see #14 for how to include themes |
pyodbc |
OK |
|
PyOpenGL 2.x |
OK |
|
PyOpenGL 3.x |
OK |
Works correctly since 3.0 rc1 |
PyQt 3 |
OK |
|
PyQt 4 |
OK |
Full support, including plugins (they are automatically handled by PyInstaller). Open bug reports: |
PyQwt 5 |
OK |
Automatically detects the correct numeric library it has been compiled with. |
PySerial |
OK |
|
OK |
Reported to work correctly in 2.0. |
|
pytz |
OK |
development branch |
pywin32 |
OK |
|
pywinauto |
OK |
|
pywintypes |
OK |
|
SciPy? |
OK |
development branch |
setuptools |
OK |
|
SIP |
OK |
|
sphinx |
OK |
development branch |
SQLAlchemy |
OK |
|
wxPython |
OK |
Remember to use windowed mode to get correct theming. |
2 使用方法
2.1 对Python脚本进行打包
PyInstaller不需要进行安装就可以使用,如果要对编写好的脚本,比如myprogram.py,进行打包,PyInstaller会对myprogram.py中用到的其它依赖包进行自动关联将所需学习全部打包到一个文件夹下,使用时可以将该文件拷贝到其它无Python环境中,亦可以正常执行,打包myprogram.py方法如下:进入到PyInstaller软件包所在的路径
pythonpyinstaller.py yourprogram.py
打包完成后会在PyInstaller包的当前路径下生成与脚本yourprogram.py同名的文件夹,即在yourprogram/dist/yourprogram下生成可执行程序,并且所有相关的依赖信息都存储在该文件夹下。
2.1 可选参数介绍
在打包时完整的命令如下:
pythonpyinstaller.py [opts] yourprogram.py
可选参数可以放在[opts]位置,各个主要、常用参数的功能介绍如下:
-F, --onefile |
将结果打包成一个可执行文件 |
-D, --onedir |
将所有结果打包到一个文件夹中,该文件夹中包括一个可以执行文件和可执行文件执行时需要的依赖文件。此中方式是默认的打包方式 |
-o DIR, --out=DIR |
打包的结果文件所在的路径,不设置此项时,打包路径在pyinstaller安装包下面 |
--additional-hooks-dir=HOOKSPATH |
有时候打包过程无错误,但是执行时报找不到某些依赖的包文件,就可以使用该参数设置,下面会看到该参数如何使用 |
|
|
3 用PyInstaller打包Python开发的Webservice程序
3.1 Webservice程序
该程序不做过多介绍,脚本如下
import soaplib
fromsoaplib.core.service import rpc, DefinitionBase
fromsoaplib.core.model.primitive import String, Integer
fromsoaplib.core.server import wsgi
fromsoaplib.core.model.clazz import Array
fromsoaplib.core.service import soap
import sys
import numpy asnp
fromscipy.optimize import fminbound
import random
import time
global xx
global xy
start_time =time.clock()
def func2(i, j):
return (i+1) * ( j+1)
defreadRealSexData(path,split_str = '^'):
#0: male; 1: female
'''
Reading sex data from path.
path: the location of sex data.
split_str: the string to be used when splitthe string.
'''
global data
global lb
global ub
lb = 10000
ub = -10000
data = list()
reader = file(path, 'r')
for line in reader:
eles = line.split(split_str)
if(len(eles) < 3):
continue
try:
first = float(eles[1])
second = int(eles[2])
if(second == 1):
data.append([first, 1])
elif(second == 0):
data.append([first,0])
if(first < lb):
lb = first
if(first > ub):
ub = first
except:
print ('The bad line: %s' %line)
continue
return data
def f(F):
'''
The objective function used buyoptimization algrithom.
F: the design variable.
'''
obj = 0.0
for ele in data:
if(ele[0] > F):
if(ele[1] == 1):
obj = obj + 1
else:
if(ele[1] == 0 ):
obj = obj + 1
return obj
defplotTrainingData(ax):
s_x = np.fromfunction(func2, (len(data),1))
s_y =np.arange(0.0,len(data))
i = 0
for ele in data:
s_x[i][0]= np.array([ele[0]])
s_y[i] = ele[1]
i += 1
ax.scatter(s_x, s_y)
defevaluation(threshold):
male = 0
female = 0
male_t = 0
female_t = 0
for ele in data:
if(ele[0] < threshold and ele[1] == 0):
male += 1
elif(ele[0] > threshold and ele[1]== 1):
female += 1
if(ele[1] == 0 ):
male_t += 1
elif(ele[1] == 1):
female_t += 1
res = dict()
res['male'] = [male_t, male, float(male )/male_t]
res['female'] = [female_t, female,float(female) / female_t]
res['total_error'] = [male + female,float(male+ female) / len(data)]
return res
class sexModelThresholdService(DefinitionBase):
@soap(String,_returns=Array(String))
def getThreshold(self, sex_file_path):
readRealSexData(sex_file_path)#'fact_user_sex_model.txt'
res = []
try:
f_res = fminbound(f, lb,ub)
except:
print 'Calling the optinizationalgrithon(minimize_scalar) failed.'
sys.exit()
print f_res
res.append(str(f_res))
return res
@soap(String,Integer,_returns=Array(String))
def say_hello(self,name,times):
results = []
for i in range(0,times):
results.append('Hello, %s'%name)
return results
if__name__=='__main__':
try:
from wsgiref.simple_server importmake_server
soap_application =soaplib.core.Application([sexModelThresholdService], 'tns')
wsgi_application = wsgi.Application(soap_application)
server = make_server('192.168.10.141',7781, wsgi_application)
server.serve_forever()
except ImportError:
print "Error: example server coderequires Python >= 2.5"
对该脚本执行打包命令:
pythonpyinstaller.py -o /home/zhongchao/workspace/python/pyinstaller/home/zhongchao/workspace/python/pyinstaller/webservece_for_sex_model_threshold.py
打包过程无错误,执行时报找不到scipy.sparse.csgraph._validation包,实际上该包在环境中是存在的,将执行方式该为如下,加上--hidden-import指令,如下
pythonpyinstaller.py --hidden-import=scipy.sparse.csgraph._validation -o/home/zhongchao/workspace/python/pyinstaller/home/zhongchao/workspace/python/pyinstaller/webservece_for_sex_model_threshold.py
重新打包,无误。在Windows访问Ubuntu上的服