python文件打包exe
简易操作
anaconda 安装: conda install pyinstaller
也可以用pip:pip install pyinstaller
然后执行pyinstaller -D xx.py
.spec
当执行完成后,会生成一个xx.spec的文件,这个文件可以相当于描述语言,下次直接执行python xx.spec就可以打包了。
高级版:如果要打包资源文件,则修改.spec文件,比如源文件是:
# -*- mode: python ; coding: utf-8 -*-
block_cipher = None
a = Analysis(['index.py'],
pathex=['D:\\workspace\\python\\tools'],
binaries=[],
datas=[],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
[],
exclude_binaries=True,
name='index',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
console=True )
coll = COLLECT(exe,
a.binaries,
a.zipfiles,
a.datas,
strip=False,
upx=True,
upx_exclude=[],
name='index')
加上add_files配置:
# -*- mode: python ; coding: utf-8 -*-
block_cipher = None
# 此处为增加代码
add_files = [
("./table/increment", "table/increment")
]
a = Analysis(['index.py'],
pathex=['D:\\workspace\\python\\tools'],
binaries=[],
datas=add_files, # 此处为修改代码
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
[],
exclude_binaries=True,
name='index',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
console=True )
coll = COLLECT(exe,
a.binaries,
a.zipfiles,
a.datas,
strip=False,
upx=True,
upx_exclude=[],
name='index')
上面是二级文件夹的合并打包方式
然后pyinstaller xx.spec即可
help
-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 pyinstall -i ***.ico **.py 添加图标 ***
pyinstaller带图标打包后图标没变化,解决方法:
1、将打包的exe文件复制到另外的一个地方,有时重启explorer.exe进程也可以修复。
2、更改文件查看方式即 调整为大图标、中图标、小图标等,explorer.exe进程会更新缓存,【注:单纯刷新不解决问题!】
还没有评论,来说两句吧...