问题汇总
- 1.打包问题
- 1.1打包后报错
- 1.1.1 Pgzero
- 1.1.1.1KeyError: "No 'images' directory found to load image 'img.png'."[36616] Failed to execute script 'file' due to unhandled exception!
- 1.1.1.1.1 问题描述
- 1.1.1.1.2 问题解释
- 1.1.1.1.3 问题解决方案
- 2.常规运行问题
- 2.1 运行报错
- 2.1.1 内置问题
- 2.1.1.1 Traceback (most recent call last):File "<pyshell#1>", line 1, in <'module'>"10+"10"TypeError: unsupported operand type(s) for +: 'int' and 'str'
- 2.1.1.1.1 问题描述
- 2.1.1.1.2 问题解释
- 2.1.1.1.3 问题解决方案
1.打包问题
1.1打包后报错
1.1.1 Pgzero
1.1.1.1KeyError: “No ‘images’ directory found to load image ‘img.png’.”[36616] Failed to execute script ‘file’ due to unhandled exception!
1.1.1.1.1 问题描述
用pyinstaller1打包后执行exe报错:
Traceback (most recent call last):File "UI.py", line 266, in <module>File "pgzero\actor.py", line 88, in __init__File "pgzero\actor.py", line 103, in __setattr__File "pgzero\actor.py", line 218, in imageFile "pgzero\loaders.py", line 120, in loadFile "pgzero\loaders.py", line 99, in validate_root
KeyError: "No 'images' directory found to load image 'image'."
[36616] Failed to execute script 'UI' due to unhandled exception!
1.1.1.1.2 问题解释
找不到images目录来打开图片(或者更多类型的,比如sounds等),因此抛出错误
1.1.1.1.3 问题解决方案
用一下命令进行打包:
pyinstaller -F --add-data "images;images" file.py
这里的images可以改成sound或music这类Pyzrun的所需文件夹2。
2.常规运行问题
2.1 运行报错
2.1.1 内置问题
2.1.1.1 Traceback (most recent call last):File “<pyshell#1>”, line 1, in <‘module’>"10+"10"TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’
2.1.1.1.1 问题描述
在python运行:
python">print(10+"10")
Traceback (most recent call last):File "<pyshell#1>", line 1, in <module>print(a+"10")
TypeError: unsupported operand type(s) for +: 'int' and 'str'
2.1.1.1.2 问题解释
此问题的主要问题是python在打印 数字10
和 字符串10
相加时出错
python中可以这样写:
python">print("10"+"10") #字符串相加
#output:1010
这是类似拼接的形式
或是数字之间的相加
python">print(10+10) #数字相加
#output: 20
但是python不支持不同类型的相加比如:
- 数字+字符(串) int/float + str
- 布尔+字符(串) bool + str
- 列表+数字 list + int/float
- 列表+字符(串) list + str
- …
这不是全部,因为python的类型很多
2.1.1.1.3 问题解决方案
在相加之前做类型转换:
python">print(str(10)+"10") #数字转为字符串
#output: 1010
print(10+int("10")) #字符串转为数字
#output: 20
注:其他类型也可以只要能转换
关于pyinstaller:https://blog.csdn.net/2301_81355627/article/details/137756856 ↩︎
这里的方法同样适用于其他同类报错 ↩︎