原本使用直接调用方式,打包时报错
FileNotFoundError: [Errno 2] No such file or directory:
0. 解决步骤
- 背景图调用方式更改
- 使用pyinstaller 对主程序打包,生成.spec 文件
- 更改.spec文件
- 重新使用pyinstaller 通过.spec 文件打包
1. 图片路径更改
1.1 更改图片路径
为方便打包,将待使用文件nicubunu_Lock.png
放至res文件夹下。
1.2背景图嵌入方式
如果希望将图片共同打包,需将原直接调用图片的路径改为相对路径。增加get_resource_path(relative_path)
,并将原程序中图片调用路径更改。
# ------------------ res path setting ----------------------- #
"""
to intergrate the image used as background in to executable program
"""
def get_resource_path(relative_path):
if getattr(sys, 'frozen', False):
base_path = sys._MEIPASS
else:
base_path = os.path.abspath('.')
return os.path.join(base_path, relative_path)
图片调用程序更改为:
# Load an image in the script
img_path = get_resource_path(os.path.join('res', "nicubunu_Lock.png"))
img = Image.open(i