一.安装Pyinstaller
使用python自带的pip命令安装,在命令行输入以下命令:
pip install pyinstaller
也可使用命令行:
pip3 install pyinstaller -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com
成功之后将看到以下的输出结果:
Successfully installed pyinstaller-xxx #xxx版本号
二.Pyinstaller生成可执行程序
pyinstaller 支持的常用选项
-h,–help | 查看该模块的帮助信息 |
---|---|
-F,-onefile | 产生单个的可执行文件 |
-D,–onedir | 产生一个目录(包含多个文件)作为可执行程序 |
-a,–ascii | 不包含 Unicode 字符集支持 |
-d,–debug | 产生 debug 版本的可执行文件 |
-w,–windowed,–noconsolc | 指定程序运行时不显示命令行窗口(仅对 Windows 有效) |
-c,–nowindowed,–console | 指定使用命令行窗口运行程序(仅对 Windows 有效) |
-o DIR,–out=DIR | 指定 spec 文件的生成目录。如果没有指定,则默认使用当前目录来生成 spec 文件 |
-p DIR,–path=DIR | 设置 Python 导入模块的路径(和设置 PYTHONPATH 环境变量的作用相似)。也可使用路径分隔符(Windows 使用分号,Linux 使用冒号)来分隔多个路径 |
-n NAME,–name=NAME | 指定项目(产生的 spec)名字。如果省略该选项,那么第一个脚本的主文件名将作为 spec 的名字 |
–i con=FILE.ICO | 加入图标(Windows Only) |
常用的-F将单个py文件生成可执行文件
在cmd窗口输入:
pyinstaller -F D:\name.py #转化的py文件的绝对路径
生成的exe文件将在tist文件夹中,C:\Users\78020\dist
将多个py文件和其他文件(音频,图片等)打包生成dist文件夹(-D)
1.spec文件(python的一个默认脚本)的生成
为了能够自定义配置的打包文件,首先需要改写spec文件,将改写好的文件保存为后缀名为(.spec),记好保存的路径
spec的初始代码
# -*- mode: python -*-
block_cipher = None
a = Analysis(['fastplot.py'],
pathex=['D:\\install_test\\DAGUI-0.1\\bin'],
binaries=[],
datas=[],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
exclude_binaries=True,
name='fastplot',
debug=False,
strip=False,
upx=True,
console=False )
coll = COLLECT(exe,
a.binaries,
a.zipfiles,
a.datas,
strip=False,
upx=True,
name='fastplot')
spec文件中主要包含4个class: Analysis, PYZ, EXE和COLLECT.
Analysis以py文件为输入,它会分析py文件的依赖模块,并生成相应的信息
PYZ是一个.pyz的压缩包,包含程序运行需要的所有依赖
EXE根据上面两项生成
COLLECT生成其他部分的输出文件夹,COLLECT也可以没有
需要改写的代码块有:
a = Analysis() , 用列表的方式存储你要打包的所有py文件路径
datas = [ ], 存储其他文件夹(事先把它们以文件夹保存好)的路径
修改之后代码:
# -*- mode: python -*-
import sys
import os.path as osp
sys.setrecursionlimit(5000)
block_cipher = None
SETUP_DIR = 'D:\\gggg\\'
a = Analysis(['D:\\gggg\\bullet.py','D:\\gggg\\main.py','D:\\gggg\\myplane.py','D:\\gggg\\supply.py','D:\\gggg\\enemy.py',],
pathex=['D:\\'],
binaries=[],
datas=[(SETUP_DIR+'font','font'),(SETUP_DIR+'images','images'),(SETUP_DIR+'plane','plane'),(SETUP_DIR+'sound','sound'),(SETUP_DIR+'text','text')],
hiddenimports=['pandas','pandas._libs','pandas._libs.tslibs.np_datetime','pandas._libs.tslibs.timedeltas',
'pandas._libs.tslibs.nattype', 'pandas._libs.skiplist','scipy._lib','scipy._lib.messagestream'],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
exclude_binaries=True,
name='fastplot',
debug=False,
strip=False,
upx=True,
console=True)
coll = COLLECT(exe,
a.binaries,
a.zipfiles,
a.datas,
strip=False,
upx=True,
name='fastplot')
2.使用spec执行打包命令
pyinstaller -D xxx.spec #xxx为文件路径
打包生成两个文件目录build和dist,
build为临时文件目录,dist(C:\Users\78020\dist)是打包的结果,可执行文件和其它程序运行的关联文件都在这个目录下,在这个文件下,即使在没有安装python的电脑也可以运行程序