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

news/2024/10/22 15:30:42/

问题汇总

  • 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

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


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

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


http://www.ppmy.cn/news/1439676.html

相关文章

为什么 Facebook 不使用 Git?

在编程的世界里&#xff0c;Git 就像水一样常见&#xff0c;以至于我们认为它是创建和管理代码更改的唯一可行的工具。 前 Facebook 员工&#xff0c;2024 年 首先&#xff0c;我为什么关心&#xff1f; 我致力于构建 Graphite&#xff0c;它从根本上受到 Facebook 内部工具的…

scss基础和css扩展

变量 定义变量 //app.scss $allpadding:20px; //声明颜色变量 $color//使用 import /assets/app.scss;.container{width: 100%;padding:$allpadding;} ⚠️scss中&#xff0c;中下划线和下划线是同一个东西 $link-color: blue; a {color: $link_color; }//编译后a {color: …

Go语言中,两个比较流行的缓存库

在 Go 中实现带有过期时间的缓存通常需要一个可以自动处理键值过期的缓存系统。虽然标准库中没有直接提供这种功能&#xff0c;但有几个流行的第三方库可以很好地满足这一需求。下面我会介绍两个比较流行的 Go 缓存库&#xff1a;go-cache 和 bigcache。 1. go-cache go-cache…

DaPy:实现数据分析与处理

DaPy&#xff1a;实现数据分析与处理 DaPy是一个用于数据分析和处理的Python库&#xff0c;它提供了一系列强大的工具和功能&#xff0c;使开发者能够高效地进行数据清洗、转换和分析。本文将深入解析DaPy库的特点、功能以及使用示例&#xff0c;帮助读者了解如何利用DaPy库处理…

目标检测——YOLOv6算法解读

论文&#xff1a;YOLOv6: A Single-Stage Object Detection Framework for Industrial Applications (2022.9.7) 作者&#xff1a;Chuyi Li, Lulu Li, Hongliang Jiang, Kaiheng Weng, Yifei Geng, Liang Li, Zaidan Ke, Qingyuan Li, Meng Cheng, Weiqiang Nie, Yiduo Li, Bo …

比特币中用到的密码学功能【区块链学习笔记1】

想要转到web3&#xff0c;这次学习区块链看的课是北大肖臻老师的公开课。 比特币又称是加密货币crypto-currency&#xff0c;但其实是不加密的&#xff0c;全是公开的。比特币中用到的密码学中HASH和签名。 1. HASH 密码学中的哈希&#xff1a;cryptographic hash function …

Android 面试题集锦

和你一起终身学习&#xff0c;这里是程序员Android Android是一种基于Linux的自由及开放源代码的操作系统&#xff0c;主要使用于移动设备&#xff0c;如智能手机和平板电脑&#xff0c;由Google公司和开放手机联盟领导及开发。这里会不断收集和更新Android基础相关的面试题&am…

伪装目标检测中数据集的标注格式:COCO和VOC

1.OSFormer中提供的COD10K的json格式&#xff0c;是coco的格式&#xff0c;但由于伪装目标检测任务的特殊性&#xff0c;标注信息中还有一个segmentation段 "images": [{"id": 3039,"file_name": "COD10K-CAM-1-Aquatic-1-BatFish-3.jpg&qu…