Python问题(报错)大全 - 不定时更新

文章讲述了使用PyInstaller打包游戏或应用时遇到的图像加载错误(KeyError)和运行时类型错误(TypeError),并提供了相应的解决方案。同时,还讨论了如何处理Python中不同类型数据相加的问题以及适用场景。
摘要由CSDN通过智能技术生成

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 image
  File "pgzero\loaders.py", line 120, in load
  File "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运行:

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中可以这样写:

print("10"+"10")  #字符串相加
#output:1010

这是类似拼接的形式

或是数字之间的相加

print(10+10)   #数字相加
#output: 20

但是python不支持不同类型的相加比如:

  • 数字+字符(串) int/float + str
  • 布尔+字符(串) bool + str
  • 列表+数字 list + int/float
  • 列表+字符(串) list + str

  • 这不是全部,因为python的类型很多
2.1.1.1.3 问题解决方案

在相加之前做类型转换:

print(str(10)+"10")    #数字转为字符串
#output: 1010
print(10+int("10"))    #字符串转为数字
#output: 20

注:其他类型也可以只要能转换


  1. 关于pyinstaller:https://blog.csdn.net/2301_81355627/article/details/137756856 ↩︎

  2. 这里的方法同样适用于其他同类报错 ↩︎

  • 11
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值