1.如果 使用 pyinstaller
1.1.打包成文件夹的命令
pyinstaller main.py
打包完成后,会生成dist目录,在目录里面会有一个main.exe可执行文件, 此时点击main.exe不一定能正常运行,若不能正常运行,比如出现闪退。使用CMD 命令窗口,在里面执行main.exe,此时就会在命令窗口输出相应的错误信息,这时就根据错误信息查找原因。
1.2pyinstaller 打包经常会出现site-packages可以找到安装的模块,但运行时却报 找不到相应模块,例如找不到模块quadpack,可以使用:
--hidden-import scipy.integrate.quadpack 来消除这个错误信息。
2.使用py2exe打包
2.1 先创建setup.py 文件
例如:使用PYQT4制作的一个界面程序
# -*- coding: utf-8 -*-
#py2exe:setup.py
from distutils.core import setup
import py2exe
#We need to import the glob module to search for all files.
import glob
import sys
#this allows to run it with a simple double click.
sys.argv.append("py2exe")
# We need to exclude matplotlib backends not being used by this executable. You may find
# that you need different excludes to create a working executable withyour chosen backend.
# We also need to include include various numerix libraries that the other functions call.
opts= {"py2exe":{ "includes" : [ "sip", "matplotlib.backends", "matplotlib.backends.backend_tkagg",
"matplotlib.figure","numpy", "matplotlib.pyplot", "pylab", "six",
"matplotlib.backend_bases", "scipy.special._ufuncs_cxx",
"scipy.integrate","scipy.integrate.quadpack","scipy.sparse.csgraph._validation"],
"excludes" : ["_gtkagg", "_tkagg","_agg2","_cairo", "_cocoaagg",
"_fltkagg","_gtk", "_gtkcairo"],
"dll_excludes":["libgdk-win32-2.0-0.dll","libgobject-2.0-0.dll","MSVCP90.dll",]}}
#Save matplotlib-data to mpl-data ( It is located in thematplotlib\mpl-data
#folder and the compiled programs will look for it in \mpl-data
#note: using matplotlib.get_mpldata_info
data_files= [(r"mpl-data",glob.glob(r"C:\Anaconda\Lib\site-packages\matplotlib\mpl-data\*.*")),
#Because matplotlibrc does not have an extension, glob does not findit (at least I think that‘s why)
#So add it manually here:
(r"mpl-data",[r"C:\Anaconda\Lib\site-packages\matplotlib\mpl-data\matplotlibrc"]),
(r"mpl-data\images",glob.glob(r"C:\Anaconda\Lib\site-packages\matplotlib\mpl-data\images\*.*")),
(r"mpl-data\stylelib",glob.glob(r"C:\Anaconda\Lib\site-packages\matplotlib\mpl-data\stylelib\*.*")),
(r"mpl-data\fonts",glob.glob(r"C:\Anaconda\Lib\site-packages\matplotlib\mpl-data\fonts\*.*"))]
#for console program use "console = [{"script" :"comtrade.py"}]
setup(
#console = [{"script" : "main.py"}],
name = "main",
version = "1.0",
windows = [{"script":"main.py"} ],
options=opts, data_files=data_files)
2.2 执行 python setup.py py2exe
打包成功后,会创建 dist 文件夹,运行里面的main.exe,若出错,错误信息记录在main.exe.log中
2.3 使用py2exe打包经常出现的错误就是找不到某个模块, 如果真没有安装,则进行安装。如果已经安装,却提示找不到。有以下几点:
(1).如果模块是以.egg方式安装的, py2exe是找不到的, 此时可以在网上找解压此模块的方法,或者安装模块的时候 使用:python setup.py install_lib 的方式安装。
(2).如果找不到的模块在 site-packages 里面的子目录下面,可以在setup.py 中的 includes 中添加模块的相对路径。
例如:使用scipy 的时候,就提示找不到 模块 _ufuncs_cxx,而此模块就在C:\Anaconda\Lib\site-packages\scipy\special\下面。此时在setup.py中加入此模块的相对路径,其它模块类似处理。
setup(windows=[{"script":"main.py"}], options={"py2exe":{"includes":["sip","scipy.special._ufuncs_cxx"]}})