Python 错误
50 个错误
Python 常见错误排查手册:缩进错误、类型错误、模块导入失败等问题的一步一步修复指南。
50 / 50
- 1
SyntaxError: 无效语法
Python 能't parse 你的 code — 那里's missing colon, parenthesis, or 错误 keyword somewhere on reported 行.
- 2
NameError: name 'x' 是不 定义
Python doesn't recognize name 你 used — 它是 从不 assigned, imported, or 定义 in current 作用域.
- 3
TypeError: unsupported operand 类型(s)
你 used 值使用运算符 or 函数那个 doesn't 接受那个类型 — 如添加字符串到 integer.
- 4
IndentationError: expected indented 块
Python uses indentation 到定义 code 块 — 错误, missing, or inconsistent indentation breaks 结构.
- 5
IndexError: 列表索引 out 的范围
你 tried 到 access 列表元素 at 索引那个是 超出列表's 有效范围 (0 到 `len(列表) - 1`).
- 6
KeyError: 'x'
你 tried 到获取值 从 dictionary using 键那个 isn't in dictionary.
- 7
ModuleNotFoundError: 否模块 named 'x'
Python 能't 查找模块你're trying 到导入 — 它's 不安装, 不 in Python path, or name 是错误.
- 8
AttributeError: 'x' 对象有 否 attribute 'y'
你 tried 到 use 方法 or 属性 on 对象那个 doesn't 有它 — 通常 typo or 错误对象类型.
- 9
ValueError: 无效 literal for int() 使用基础 10
你通过值 的正确类型但 使用无效内容 — 如 converting 非-numeric 字符串到 int.
- 10
ZeroDivisionError: division 通过零
你 attempted 到划分数字通过零, 哪个是 mathematically undefined.
- 11
FileNotFoundError: [Errno 2] 否 such file or directory
你 tried 到打开, 读取, or access file 那个 doesn't exist at specified path.
- 12
StopIteration
你 called 下一个() on iterator 那个有 否更多值 到 produce.
- 13
RecursionError: 最大值 recursion 深度 exceeded
你的 recursive 函数 exceeded Python's 最大值 call stack 深度 (默认 ~1000), 通常 due 到 missing or 不正确基础情况.
- 14
OverflowError: math 范围错误
calculation produced 数字也 大 for Python 到 represent, 经常 in float 操作.
- 15
UnicodeDecodeError: 'utf-8' codec 能't decode 字节
你 tried 到 decode 字节 using encoding (如 UTF-8) 那个 doesn't match 实际字节内容.
- 16
UnicodeEncodeError: 'ascii' codec 能't encode character
你 tried 到 encode 字符串到 encoding 那个能't represent 一些的 它的 characters.
- 17
PermissionError: [Errno 13] Permission 拒绝
你 tried 到读取, 编写, or 执行 file or resource 不使用必要 permissions.
- 18
AssertionError
assert 语句检查条件那个 evaluated 到假, indicating programming bug.
- 19
NotImplementedError
你 called 方法 or 函数那个是 定义但 不 yet implemented — placeholder 那个需要到 是 filled in.
- 20
RuntimeError
错误 occurred 期间 execution 那个 doesn't fit 更多特定异常分类.
- 21
UnboundLocalError: 局部变量 'x' referenced 之前 assignment
你 referenced 局部变量之前 assigning 值到 它, 通常 due 到 conditional assignment path.
- 22
TabError: inconsistent use 的 tabs and 空间 in indentation
你的 file mixes tabs and 空间 for indentation in 方式那个 Python 能't unambiguously 解释执行.
- 23
ValueError: 不 enough 值到 unpack
你 tried 到 unpack 序列到 数字的 变量那个 doesn't match 序列's 长度.
- 24
MemoryError
你的 program tried 到 allocate 更多 memory than 是 available.
- 25
OSError: [Errno 28] 否空间左 on device
操作系统错误 occurred — 特定 subtype (FileNotFoundError, PermissionError, etc.) gives 更多 detail.
- 26
ConnectionError
network 连接能 不是 established or 是 lost — 服务器可能是 下 or unreachable.
- 27
TimeoutError
network or I/O 操作 exceeded 它的时间限制 and 是 terminated.
- 28
JSON.decoder.JSONDecodeError: Expecting 值
你 tried 到 parse 字符串 as JSON, 但字符串 isn't 有效 JSON 格式.
- 29
ImportError: cannot 导入 name 'X' 从 'Y'
你 tried 到导入 name 从模块, 但 name doesn't exist in 那个模块 or 那里's circular 导入.
- 30
DeprecationWarning
你的 code uses deprecated feature 那个 still 工作但 将是 移除 or 更改 in future Python 发布.
- 31
RecursionError:超过最大递归深度
你的递归函数永远不会停止调用自身 —— Python 达到默认递归深度限制(通常 1000)并抛出 RecursionError。
- 32
FileNotFoundError:没有此文件或目录
你尝试打开一个不存在的文件 —— Python 抛出 FileNotFoundError 并附带找不到的路径。
- 33
PermissionError:操作不允许
你尝试读写或执行一个进程没有权限的文件 —— Python 抛出 PermissionError。
- 34
手动 next() 调用引发 StopIteration
你对没有更多元素的迭代器调用了 next() —— Python 抛出 StopIteration,如果未捕获会导致程序崩溃。
- 35
KeyError:字典中找不到键
你用 dict[key] 访问了字典中不存在的键 —— Python 抛出 KeyError 并以缺失的键为参数。
- 36
AttributeError:对象没有该属性
你尝试访问 obj.attribute 或调用 obj.method(),但该属性或方法在该对象类型上不存在。
- 37
ValueError:int() 的无效字面量
你用无法解析为数字的字符串调用了 int()、float() 等 —— Python 抛出 ValueError 并附带无效输入。
- 38
IndentationError:意外缩进
你的代码缩进不一致 —— Python 用缩进定义块,空格不对齐时抛出 IndentationError。
- 39
TabError:制表符和空格使用不一致
你的文件在同一块中同时使用制表符和空格缩进 —— Python 无法确定缩进级别并抛出 TabError。
- 40
SyntaxError:f-string 表达式不能包含反斜杠
你在 f-string 花括号内使用了反斜杠 —— Python 3.11 及更早版本拒绝此操作并抛出 SyntaxError。
- 41
SyntaxError:海象运算符语法无效
你在 Python 不支持的上下文中使用了海象运算符 :=,或在 Python 3.7 或更早版本上使用它。
- 42
SyntaxError:match 语句语法无效
你在 Python 3.9 或更早版本上使用了 match/case 语句 —— 结构化模式匹配需要 Python 3.10+。
- 43
SyntaxError:装饰器后缺少函数或类
你写了 @decorator 但下一行不是 def 或 class —— Python 期望装饰器后跟函数或类定义。
- 44
可变默认参数陷阱
你使用了 def f(x=[]) 或 def f(x={}) —— 默认值在定义时创建一次,在所有使用默认值的调用间共享。
- 45
整数除法截断陷阱
你使用 // 并得到了意外结果,因为向下取整除法向负无穷舍入,而非像截断那样向零舍入。
- 46
is 与 == 比较混淆
你用 'a is b' 但本意是 'a == b' —— 'is' 检查两个变量是否指向同一对象,而非值是否相等。
- 47
浅拷贝导致嵌套引用共享
你使用 copy.copy() 并修改了嵌套对象 —— 原始和副本共享同一嵌套引用,两者都受影响。
- 48
range() 循环的差一错误
你的循环迭代次数多一或少一,因为 range() 排除终止值 —— range(5) 给出 0..4 而非 0..5。
- 49
字符串不可变性:方法返回新字符串
你调用 s.upper() 或 s.replace() 但未赋值结果 —— 字符串不可变,原始不变。
- 50
浮点精度:0.1 + 0.2 != 0.3
你用 == 比较浮点数得到 False,因为 0.1 + 0.2 产生 0.30000000000000004 而非精确的 0.3。
未找到相关错误。